问题
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.
回答1:
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
回答2:
You can use tidy
and augment
from the broom package to take the results from plm
and put them into a data frame.
library(plm)
library(broom)
data("Grunfeld", package = "plm")
p <- plm(inv ~ value + capital, data = Grunfeld, model = "pooling")
df <- tidy(p)
# term estimate std.error statistic p.value
# 1 (Intercept) -42.7143694 9.51167603 -4.490730 1.207357e-05
# 2 value 0.1155622 0.00583571 19.802589 9.542703e-49
# 3 capital 0.2306785 0.02547580 9.054808 1.347370e-16
df$estimate
# [1] -42.7143694 0.1155622 0.2306785
augment(p)
# inv value capital .fitted .resid
# 1 317.60 3078.50 2.80 313.689629 3.910371
# 2 391.80 4661.70 52.60 508.135423 -116.335423
# 3 410.60 5387.10 156.90 616.023978 -205.423978
# 4 257.70 2792.20 209.20 328.216223 -70.516223
# 5 330.80 4313.20 203.40 502.648328 -171.848328
# 6 461.20 4643.90 207.20 541.741311 -80.541311
# 7 512.00 4551.20 255.20 542.101267 -30.101267
# 8 448.00 3244.10 303.70 402.237879 45.762121
# 9 499.60 4053.70 264.10 486.662133 12.937867
# 10 547.50 4379.30 201.60 509.871765 37.628235
来源:https://stackoverflow.com/questions/46232076/extract-all-individual-slope-coefficient-from-pooled-ols-estimation-in-r