Predict values from multivariate linear model

I need help predicting a value from new data, from a multivariate lm model.

project_model <- lm(project_data$Log.Odds.Ratio ~ project_data$Complexity.Level + project_data$Product.Type + project_data$Plant.Normalized.Hours + project_data$Norm.Sq.Ft)

(Four predictors)

I want to predict the Log.Odds.Ratio from a new set of data. (Same column names)

new_data <- data.frame(Complexity.Level = 3,Product.Type = "End",Plant.Normalized.Hours = 1.5 ,Norm.Sq.Ft = -0.6458333)
new_data[1,1] <- factor(new_data[1,1])
predict(project_model,new_data,interval='confidence')

However, I get this error

Warning message:
'newdata' had 1 row but variables found have 260 rows 

Along with the 260 predicted values from the data set I used to fit the model, but none are from the new data I have.

I have done

mtcars <- mtcars mtcars$cyl <- factor(mtcars$cyl)

mtcars.lm <- lm(mpg ~ hp + cyl + disp + vs, data = mtcars)

# hp = 110
# cyl = 6
# disp = 200
# vs = 1

new_data <- data.frame(hp = 110, cyl = 6, disp = 200, vs = 1)
new_data$cyl <- factor(new_data$cyl)

predict(mtcars.lm,new_data,interval = 'confidence')

And it works! I don't know what is the difference, but for some reason I can't get it to work with my actual data.

链接地址: http://www.djcxy.com/p/24840.html

上一篇: 在Shiny中通过输入变量计算平均值的错误

下一篇: 预测多元线性模型的值