Predicting forecasting a regression

Good morning everyone

I am applying fixed effects (o random) to a panel data. Please, after that, does a "predict" function exist?

A "predict" function that works in the same way it does with the linear regression "lm".

For a linear regression:
predict(lm.output, data.frame(x), interval="prediction", level=0.95)

For e fixed effects o random? Does a predict function exist?

I have already tried to use the function "prediction" (maybe I am not applying the right inputs (find.data?, parent.frame?, at?)) and I tried the function "prophecy.plm.out" (where I have problems installing libraries such as plmNeviim and spd4testing).

I think this issue is really important; a regression without future prediction is not enough.

I really thank you very much in advance.

Hi, and welcome!

Please see the FAQ: What's a reproducible example (`reprex`) and how do I do one? Using a reprex, complete with representative data will attract quicker and more answers. Not needed here, but good to keep in mind.

See this example from the predict.lm{stats} help page

x <- rnorm(15)
y <- x + rnorm(15)
predict(lm(y ~ x))
#>           1           2           3           4           5           6 
#>  0.25960090  0.91609553  0.77051055  0.27562176  0.40192878  0.58395211 
#>           7           8           9          10          11          12 
#> -0.06984815  0.07619936 -0.24433707 -1.14242924 -0.12167502 -0.29181949 
#>          13          14          15 
#>  0.59843283  0.56245740 -0.31719619
new <- data.frame(x = seq(-3, 3, 0.5))
predict(lm(y ~ x), new, se.fit = TRUE)
#> $fit
#>           1           2           3           4           5           6 
#> -1.55072900 -1.25306378 -0.95539856 -0.65773335 -0.36006813 -0.06240291 
#>           7           8           9          10          11          12 
#>  0.23526230  0.53292752  0.83059274  1.12825795  1.42592317  1.72358839 
#>          13 
#>  2.02125360 
#> 
#> $se.fit
#>         1         2         3         4         5         6         7         8 
#> 0.8581513 0.7218424 0.5890542 0.4628272 0.3503269 0.2693410 0.2523015 0.3099407 
#>         9        10        11        12        13 
#> 0.4120269 0.5336337 0.6641230 0.7991555 0.9367687 
#> 
#> $df
#> [1] 13
#> 
#> $residual.scale
#> [1] 0.9642231
pred.w.plim <- predict(lm(y ~ x), new, interval = "prediction")
pred.w.clim <- predict(lm(y ~ x), new, interval = "confidence")
matplot(new$x, cbind(pred.w.clim, pred.w.plim[,-1]),
        lty = c(1,2,2,3,3), type = "l", ylab = "predicted y")

Created on 2020-03-06 by the reprex package (v0.3.0)

Hi Tech I thank you very much
With your example I trained myself a little bit with the linear regression. A step forward.
I stll have some problem when I want to predict a plm fixed effect.

fixed = plm(y ~ x, data = Panel, index = c("entity","time"), model = "within")
Than, when I launch
predict(fixed, Panel)

It says:
"Error in crossprod(beta, t(X)) : the arguments are not compatible".

It seems there's a problem because a "plm within regression" lacks of the overall intercept.

Do you have some idea to work around this problem and predict future values in a fixed effect regression?

Thank you

The reason for pointing out the FAQ: What's a reproducible example (`reprex`) and how do I do one? was to emphasize how difficult it can be to formulate an answer without a reprex, such as this example from the plm help page

library(plm)

data("Produc", package = "plm")
zz <- plm(log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp,
              data = Produc, index = c("state","year"))
    summary(zz)
#> Oneway (individual) effect Within Model
#> 
#> Call:
#> plm(formula = log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp, 
#>     data = Produc, index = c("state", "year"))
#> 
#> Balanced Panel: n = 48, T = 17, N = 816
#> 
#> Residuals:
#>      Min.   1st Qu.    Median   3rd Qu.      Max. 
#> -0.120456 -0.023741 -0.002041  0.018144  0.174718 
#> 
#> Coefficients:
#>              Estimate  Std. Error t-value  Pr(>|t|)    
#> log(pcap) -0.02614965  0.02900158 -0.9017    0.3675    
#> log(pc)    0.29200693  0.02511967 11.6246 < 2.2e-16 ***
#> log(emp)   0.76815947  0.03009174 25.5273 < 2.2e-16 ***
#> unemp     -0.00529774  0.00098873 -5.3582 1.114e-07 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Total Sum of Squares:    18.941
#> Residual Sum of Squares: 1.1112
#> R-Squared:      0.94134
#> Adj. R-Squared: 0.93742
#> F-statistic: 3064.81 on 4 and 764 DF, p-value: < 2.22e-16

Created on 2020-03-07 by the reprex package (v0.3.0)

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.