简体   繁体   English

如何使用 R 中的多重线性模型预测未来值?

[英]How to predict future values using a multiple linear model in R?

So i currently have a data set consisting of the Year, Credit Hours, and Number of students.所以我目前有一个由年份、学分和学生人数组成的数据集。 I have been trying to predict future credit hours by the number of students.我一直试图通过学生人数来预测未来的学分。

   df <- data.frame("year = c(2018,2019,2020,2021), "student" = c(1000,1200,1350,1450), "credit" = c(4000,4300,4730,4250))


    mod <- lm(credit ~ year + student, data = df)
    summary(mod)

I would like to predict the number of credit hours for the next couple of years, lets just say 2022:2025, that also factors in predicted number of students.我想预测未来几年的学分数量,比如 2022:2025,这也是预测学生数量的因素。 Is there a way to do this?有没有办法做到这一点?

year credit信用 student学生
2018 2018 4000 4000 1000 1000
2019 2019 4300 4300 1200 1200
2020 2020 4730 4730 1350 1350
2021 2021 4250 4250 1450 1450
2022 2022 NA不适用 NA不适用
2023 2023 NA不适用 NA不适用
2024 2024 NA不适用 NA不适用
2025 2025 NA不适用 NA不适用

In other words, how can I use a linear model in R to predict all of these NA values?换句话说,我如何使用 R 中的线性模型来预测所有这些 NA 值? I can do this in a simple linear regression no problem, but cannot seem to get it to work in multiple form.我可以在简单的线性回归中做到这一点没问题,但似乎无法让它以多种形式工作。

You need to pass a dataframe to predict() as the newdata argument.您需要将数据帧作为 newdata 参数传递给 predict()。 The data frame requires you to specify values of independent variables for each prediction.数据框要求您为每个预测指定自变量的值。 If you also want to predict the number of students based on a linear model and use that as input then you can do that step first.如果您还想根据线性模型预测学生人数并将其用作输入,那么您可以先执行该步骤。 Something like:就像是:

lm.student <- lm(students ~ year, df)
pred.student <- predict(lm.student, newdata = data.frame(year=2022:2025))

mod <- lm(credit ~ year + student, data = df)
MyNewData <- data.frame (year=2022:2025, student=pred.student)
pred <- predict(mod, newdata = MyNewData)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM