0

When I try to plot my mrl_model I get an error message in xy coordinates. I do not know enough about R to troubleshoot this issue. Can someone help me to resolve this error?

set.seed(1234)
ind <- sample(2, nrow(New_imports), replace = TRUE, prob = c(0.7, 0.3))
train.data <- New_imports[ind == 1, ]
test.data <- New_imports[ind == 2, ]
myFormula<-Price~.
mlr_model<-lm(myFormula, data=train.data)
print(mlr_model)

Call:
lm(formula = myFormula, data = train.data)

Coefficients:
        (Intercept)            symboling    normalized_losses      aspirationturbo         num_doorstwo  
         -44140.550             -206.852                8.956             1538.373              -83.248  
  body_stylehardtop  body_stylehatchback      body_stylesedan      body_stylewagon      drive_wheelsfwd  
         -12119.455           -11201.288           -10659.429           -10655.282            -1466.030  
    drive_wheelsrwd  engine_locationrear           wheel_base               length                width  
            702.577             2415.164               95.239              -89.271              792.837  
             height          curb_weight          engine_size    compression_ratio             peak_rpm  
            -13.931                3.403               45.880               91.632                1.333  
           city_mpg          highway_mpg  
           -221.142              155.776  

pred <-predict(mlr_model, New_imports=test.data)
plot(test.data$Price, pred, xlab = "Observed", ylab = "Prediction") 
Error in xy.coords(x, y, xlabel, ylabel, log) : 
  'x' and 'y' lengths differ
GabJosh
  • 37
  • 3
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. I'm guessing the `pred <-predict(mlr_model, New_imports=test.data)` line is wrong. The argument for new data is `newdata=`, not `New_imports=`. Try `pred <-predict(mlr_model, newdata=test.data)` – MrFlick Jul 06 '21 at 05:26
  • it causes new errors > pred <-predict(mlr_model, newdata=test.data) Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : factor make has new levels porsche – GabJosh Jul 06 '21 at 14:31

0 Answers0