title: Model Prediction for pooled regression model in panel data
body: <p>I'm trying to produce a predictive model where i performed multiple pooled regressions in each year (based on previous years) and thus allow coefficients to vary across time. (This might not make sense in the sample data provided, but it is done in practice for my sample).</p>
  
  <p>Here is what I came up so far: I adjusted my code to a reproducible sample from the plm package: </p>
  
  <p>The data is structured in the following way (panel) with firm, year indexed.</p>
  
  <pre><code>&gt; head(Grunfeld)
    firm year   inv  value capital
  1    1 1935 317.6 3078.5     2.8
  2    1 1936 391.8 4661.7    52.6
  3    1 1937 410.6 5387.1   156.9
  4    1 1938 257.7 2792.2   209.2
  5    1 1939 330.8 4313.2   203.4
  6    1 1940 461.2 4643.9   207.2
  </code></pre>
  
  <p>and here is my code:</p>
  
  <pre><code>library(plm)
  data("Grunfeld", package="plm")
  
  # Store each subset regression in myregression
  myregression &lt;- list()
  count &lt;- 1
  
  ## pooled regression in each year t, 
  ## with subset data of the previous six years (t-5) 
  
  for(t in 1940:1950){  
    myregression[[count]] &lt;- plm(inv ~ value + capital, 
                                subset(Grunfeld, year&lt;=t &amp; year&gt;=t-5),
                                index=c("firm","year"))
  # Name each regression based on the year range included in the data subset
  names(myregression)[[count]] = paste0("Year_",t)
  count &lt;- count+1
  }
  
  
  ## Prediction
  #######################
  # Alternative 1: Loop
  
  Forecast&lt;-list()
  count&lt;-1
  for(t in 1940:1950){
    Forecast[[count]]&lt;-predict(myregression[[count]], subset(Grunfeld, year==t))
    ## Name each Prediction based on the year t:
   names(Forecast)[[count]] = paste0("Year_",t)
   count &lt;- count+1
  }
  </code></pre>
  
  <p>Unfortunately my code does not work and i get the following error:</p>
  
  <pre><code>Error in crossprod(beta, t(X)) : non-conformable arguments
  </code></pre>
  
  <p>Ideally i would like to store my Predictions/Forecasts in $Grunfeld$Forecast in the same structure as the original Grunfeld data. However I experienced a lot of difficulties working with Lists and often failed to correctly address them and store the results in a vector next to the original data. This is crucial as in my own sample, there is a lot of missing data (NA's) and i can only use the predict function on a limited subset. How do you arrange the data in a desired way?</p>
  
  <p>And is this the right approach to obtain conditional forecasts (on the year)with varying slopes and storing them in the same manner as the original data or are there more efficient ways i'm unaware of? </p>