rlmpredictpoly

Predicting multivariant linear model in R


I have a question regarding a prediction for linear model with two variants (multivariant). I am trying to use a model I trained to predict new data. I have this code that trains z in terms of x and y. The data is around 60,000 rows:

fit <- lm(z ~ poly(x, y, degree = 4), data = data)

Then I want to predict just one row by doing the following

x <- -20
y <-  20
data2 <- data.frame(cbind(x,y))
prediction <- predict(fit,data2)

The r throughs this error:

Error in Z[, 2] <- x - alpha[1L] : replacement has length zero

Thanks for your help!


Solution

  • The following code using the reproducible input shown in the Note at the end runs without any error messages.

    fit <- lm(z ~ poly(cbind(x, y), degree = 4), data = data)
    
    data2 <- data.frame(x = -20, y = 20))
    predict(fit, data2)
    ##        1 
    ## 436884.4
    

    Note

    set.seed(123)
    
    n <- 25
    data <- data.frame(x = rnorm(n), y = rnorm(n), z = rnorm(n))