简体   繁体   中英

R language - predict() function - to explain

At the beginning - as I'm almost completely new to R language (and programming at all to be honest) I'd ask you to reply as simple as you can :)

The problem is - I've made a basic linear model that should be saying what will be the unemployment level in my country. It's easy as far as it comes to prediction. I mean, I have theoretical values from my model and real values for years 2011-2017. 24 observations, data gathered quarterly from 4th quarter 2011 to 3rd one 2017. And now I want to make a prediction for 1st quarter of 2018. So the code goes like that:

data <- read.csv2("http://web2.ue.katowice.pl/trzesiok/bezrobotni.csv")
trend <- lm(formula = dane$l.bezrobotnych ~ t, data = dane)
prog.df <- data.frame(t=26)
prog.I.2018 <- predict(trend, prog.df)

And now - it's all ok, it gives me a result for 1st quarter of 2018. BUT. What about 25th observation? What should I have done if I'd like to make a prediction for a 4th quarter 2040 and I'd like to have ALL of the predicions since 2017? Thanks in advance :)

If you want a larger range of quarters, you need to change the value of t when you define prog.df. If you want all of 2018, for example, you would do:

prog.df <- data.frame(t %in% 26:29)

This will store all the quarters from 26 to 29 in prog.df, so predict function will return values for all four quarters. For any other subset you want, you just need to find the corresponding quarter # or list of quarter #'s to define t.

You need to create your dataframe for predictions with all covariates for which you want a prediction. In your case, for all t .

  • For 25 and 26 : prog.df <- data.frame(t=25:26)
  • For 25 to 30 : prog.df <- data.frame(t=25:30)
  • For 25 to 100, every 4 : prog.df <- data.frame(t=seq(25, 100, 4))
  • etc...

Just be careful with predictions out of the scope of your data...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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