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)

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. Is there a way to do this?

year credit student
2018 4000 1000
2019 4300 1200
2020 4730 1350
2021 4250 1450
2022 NA NA
2023 NA NA
2024 NA NA
2025 NA NA

In other words, how can I use a linear model in R to predict all of these NA values? I can do this in a simple linear regression no problem, but cannot seem to get it to work in multiple form.

  • 1
    $\begingroup$ Build a data frame with the right column names, populated with your numbers of interest, and use the “predict” function. ?predict.lm $\endgroup$ May 12, 2022 at 1:18

1 Answer 1


To predict $credit_{t+1}$, you need $year_{t+1}$ (which you have) and $student_{t+1}$ (which you don't have). So first you need to create a model (or a formula) to predict that.

Looking are your data, the student vector is $(1000,1200,1350,1450)$ which could be (for example) a marginally decreasing series such as: $(.., 200,150,100)$ halving by each year, thus the following changes could be $(50,25,12.5,6.25)$.

Therefore the forecasted values for student could be $(1500,1525,1537.5, 1543.75)$. You can also create a trend model (in levels, $student = f(year)$ or differences, $Δstudent = f(year)$ ) for the same goal.

Now you have the values for all independent variables in the forecasting period, therefore after the predict option, you can apply the coefficients of the model to the full data to obtain the future values of credit hours. This explanation doesn't deal with the goodness of fit of that last model ($credit = f(year, student)$).


Not the answer you're looking for? Browse other questions tagged or ask your own question.