# 语法：具有不同子集的循环回归

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

library(plm)
data("Grunfeld", package="plm")

#regression
myregression <- list()
Grunfeld_sub <- data.frame()
count <- 1

#loop
for(t in 1940:1950){

Grunfeld_sub[t] <- subset(Grunfeld, year<=t & year>=t-5)
myregression[[count]] <- lm(inv~value + capital, Grunfeld_sub(t))

count<- count+1
}

eipi10

# Store each subset regression in myregression
myregression <- list()

# Regression on six-year subsets of Grunfeld
for(t in 1940:1950) {

myregression[[t-1939]] <- lm(inv ~ value + capital,
subset(Grunfeld, year<=t & year>=t-5))

# Rename list elements by year range of subset
names(myregression)[[t-1939]] = paste0("Years:",t-5,"-",t)
}

> myregression
\$`Years:1935-1940`

Call:
lm(formula = inv ~ value + capital, data = Grunfeld_sub)

Coefficients:
(Intercept)        value      capital
-3.65240      0.08283      0.11033

\$`Years:1936-1941`

Call:
lm(formula = inv ~ value + capital, data = Grunfeld_sub)

Coefficients:
(Intercept)        value      capital
-13.77258      0.08614      0.18680

for(t in 1940:1950) {

myregression[[t-1939]] <- plm(inv ~ value + capital,
data=subset(Grunfeld, year<=t & year>=t-5),
index=c("firm","year"))
names(myregression)[[t-1939]] = paste0("Years:",t-5,"-",t)
}

0 条评论