0

I fit a multivariate model with lmRob in the robust package and I like the fit. How can I use the fit to make a prediction at a given point? The hackish solution is to plot it and place horizontal and vertical lines on the plot to pinpoint

How can I feed the model a point, and have it spit back the prediction? I'm imagining it's something like:

predict(model, newdata = data.frame(x = 2, y = 90))

But this gives me the error:

predict(model, newdata = data.frame(x = 2, y = 90))
Error in `contrasts<-`(`*tmp*`, value = contrasts.arg[[nn]]) : 
  contrasts apply only to factors

The traceback() is:

> traceback()
7: stop("contrasts apply only to factors")
6: `contrasts<-`(`*tmp*`, value = contrasts.arg[[nn]])
5: model.matrix.default(delete.response(Terms), newdata, contrasts = object$contrasts, 
       xlevels = attr(object, "xlevels"))
4: model.matrix(delete.response(Terms), newdata, contrasts = object$contrasts, 
       xlevels = attr(object, "xlevels"))
3: predict.lmRob(model, newdata = data.frame(x = 1, 
       y = 90), interval = "predict")
2: predict(model, newdata = data.frame(x = 1, y = 90), 
       interval = "predict")
1: predict(model, newdata = data.frame(x = 1, y = 90), 
       interval = "predict")

If I just try passing the original data set into predict, I get:

Error in x %*% coefs : non-conformable arguments

Adding the appropriate factor levels fixes the first warning, but leaves the second.

wdkrnls
  • 4,548
  • 7
  • 36
  • 64
  • `?predict.lmRob` according to that you'd only need `predict.lmRob(model, newdata = data.frame(x = 2, y = 90))` – mts Aug 10 '15 at 20:36
  • 1
    The names and types of the variables in the data frame you use in `predict()` have to match exactly the ones you used in the original call to `lmRob()`. I can't say without seeing your data, but maybe this is the source of your problem? – ulfelder Aug 10 '15 at 20:42
  • @ulfelder: My `x` is a factor, so that could be one issue. But when I try passing in the original data set to `newdata` (e.g. `predict(model, newdata = d)` I get `Error in x %*% coefs : non-conformable arguments`, so I don't think it's the only one. – wdkrnls Aug 10 '15 at 20:49

1 Answers1

4

You need to make sure the newdata has the same levels as the original, ie.

dat <- data.frame(x=1:10, y=factor(sample(letters[1:2], 10, rep=T)),
                  z=runif(10))
fit <- lmRob(z ~ ., data=dat)

## Fails, wrong factor
predict(fit, newdata=data.frame(x=11, y="a")) 

## Works
predict(fit, newdata=data.frame(x=11, y=factor("a", levels=letters[1:2])))

Edit

You will get the second error if you do something like this

dat <- data.frame(x=1:10, y=factor(sample(letters[1:2], 10, rep=T), levels=letters[1:3]),
                  z=runif(10))  # data has empty "c" level
fit <- lmRob(z ~ ., data=dat)

## Fails
predict(fit, newdata=dat)

## Works
predict(fit, newdata=droplevels(dat))
Rorschach
  • 31,301
  • 5
  • 78
  • 129