[R] Defining multiple variables in a loop

Taylor White taylorgentrywhite at gmail.com
Sun Jun 24 23:15:01 CEST 2012


Good day,

For lack of a better solution (or perhaps I am ignorant to something
more elegant), I have been bootstrapping panel data by hand so to
speak and I would like to know if there is a way to define multiple
variables in a loop using the loop variable.  I found a post (here:
https://stat.ethz.ch/pipermail/r-help/2002-October/026305.html ) that
discussed naming multiple variables but it didn't seem to allow me to
define the variables as something more useful.  I tried the code
immediately below (plus some variations) and it just didn't work.

for (i in 1:20) {
assign(paste("country.", i, sep = "") <- subset(OECDFiscal2, Country == i)
}


I included some sample code from what I've been working on below so
one can see how it would be very useful to figure out how to define a
series of variables from cross sectional units from a panel dataset.


Any help would be much appreciated.


Thanks,

Taylor White
UCLA



######Bootstrapping panel data by hand.  Create 4 variables from 3
subsets of the original data. Resample each variable and recombine
into one matrix.


plmcoef <- array(0, c(1000, 4)) #creates an empty array to store
regression coefficients
plmfixef <- array(0, c(1000, 3)) #creates an empty array to store
fixed effects intercepts from regressions


for (i in 1:1000) {
country1 <- as.data.frame(subset(OECDFiscal2, Country == 1))
country2 <- as.data.frame(subset(OECDFiscal2, Country == 2))
country3 <- as.data.frame(subset(OECDFiscal2, Country == 3))

exp1 <- as.matrix(sample(country1$lagexpVSgdp, size =
(nrow(country1)), replace = T))
exp2 <- as.matrix(sample(country2$lagexpVSgdp, size =
(nrow(country2)), replace = T))
exp3 <- as.matrix(sample(country3$lagexpVSgdp, size =
(nrow(country3)), replace = T))

tax1 <- as.matrix(sample(country1$lagtaxVSgdp1, size =
(nrow(country1)), replace = T))
tax2 <- as.matrix(sample(country2$lagtaxVSgdp1, size =
(nrow(country2)), replace = T))
tax3 <- as.matrix(sample(country3$lagtaxVSgdp1, size =
(nrow(country3)), replace = T))

gdp1 <- as.matrix(sample(country1$yoygdpcapita, size =
(nrow(country1)), replace = T))
gdp2 <- as.matrix(sample(country2$yoygdpcapita, size =
(nrow(country2)), replace = T))
gdp3 <- as.matrix(sample(country3$yoygdpcapita, size =
(nrow(country3)), replace = T))

unemployment1 <- as.matrix(sample(country1$lagunemployment, size =
(nrow(country1)), replace = T))
unemployment2 <- as.matrix(sample(country2$lagunemployment, size =
(nrow(country2)), replace = T))
unemployment3 <- as.matrix(sample(country3$lagunemployment, size =
(nrow(country3)), replace = T))

country.year1 <- as.matrix(cbind(country1$Country, country1$Year2))
country.year2 <- as.matrix(cbind(country2$Country, country2$Year2))
country.year3 <- as.matrix(cbind(country3$Country, country3$Year2))

country1.2 <- as.data.frame(cbind(country.year1, exp1, tax1, gdp1,
unemployment1))
country2.2 <- as.data.frame(cbind(country.year2, exp2, tax2, gdp2,
unemployment2))
country3.2 <- as.data.frame(cbind(country.year3, exp3, tax3, gdp3,
unemployment3))

data <- as.data.frame(rbind(country1.2, country2.2, country3.2))

OECDsamplepanel <- pdata.frame(data, index = NULL, drop = F)

plm <- plm(V5 ~ lag(V6, 1) + V3 + V4 + V5, data = OECDSamplepanel,
model = "within")

coefficients <- t(as.matrix(plm$coefficients))
fixef <- t(as.matrix(fixef(plm)))

plmcoef[i, 1:4] = coefficients
plmfixef[i, 1:3] = fixef

}



More information about the R-help mailing list