我需要帮助从多元lm模型中预测新数据的值 .

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)

(四个预测因子)

我想从一组新数据中预测Log.Odds.Ratio . (相同的列名)

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')

但是,我收到此错误

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

连同我用来拟合模型的数据集中的260个预测值,但没有一个来自我的新数据 .

我已经做好了

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')

它的工作原理!我不知道有什么区别,但由于某种原因我无法使用我的实际数据 .