Search code examples
rplmcoefficients

Extract all individual slope coefficient from pooled OLS estimation in R


I hit a problem where I want to extract all individual coefficient of a particular variable in a pooled regression.

My data look like this and I regress Y on X.

Observation name date Y  X
1           A    1    Y1 X1
2           A    2    Y2 X2
3           B    1    Y3 X3
4           B    2    Y4 X4

Using the plm package and summary, R only gives me one coefficient of X. However, I want to have coefficient of X variable in each individual regression. Can anyone help me with this?

To clarify, what I want is all the beta associated with the X_n,1 in the below picture. Sorry for the confusion. Pictures for clarification


Solution

  • If you want different coefficients for the entities (split by name in your example), you can use the function pvcm() from the package plm to fit a OLS model per individual or do it by hand, see code example below.

    Building upon the example from the help page (?pvcm):

    library(plm)
    data("Produc", package = "plm")
    
    form <- log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp
    
    pvcm(form, data = Produc, model = "within") # individual coefficients
    ## Coefficients:
    ##                (Intercept)  log(pcap)    log(pc)  log(emp)       unemp
    ## ALABAMA            8.49604 -1.4426440  0.2795010  1.835250  0.00735450
    ## ARIZONA            4.66528 -0.1627084 -0.0052207  1.075828 -0.00365798
    ## ARKANSAS           3.24565 -0.5056503  0.3212473  1.234017  0.00149221
    ## CALIFORNIA         0.27935  0.2639377  0.2484033  0.699135 -0.01074510
    ## [...]
    
    ###### same using OLS on splitted data for first entity (ALABAMA):
    l <- split(Produc, Produc$state)
    plm(form, data = l[[1]], model = "pooling")
    ## Coefficients:
    ## (Intercept)   log(pcap)     log(pc)    log(emp)       unemp 
    ##   8.4960384  -1.4426440   0.2795010   1.8352498   0.0073545