I have a dataset of export trade data for a single country with 21 columns. The first column indicates the years (1962-2014) while the other 20 are trading partners. I am tr
Quite aside from the statistical justification for doing this, the programming problem is an interesting one. Here is a solution, but probably not the most elegant one. First, create a sample data set:
x = c(1962:2014)
y1 = c(rnorm(53))
y2 = c(rnorm(53))
y3 = c(rnorm(53))
mydata = data.frame(x, y1, y2, y3)
attach(mydata)
head(mydata)
# x y1 y2 y3
#1 1962 -0.9884054 -1.68208217 0.5980446
#2 1963 -1.0741098 0.51309753 1.0986366
#3 1964 0.1357549 -0.23427820 0.1482258
#4 1965 -0.8846920 -0.60375400 0.7162992
#5 1966 -0.5529187 0.85573739 0.5541827
#6 1967 0.4881922 -0.09360152 -0.5379037
Next, use a for loop to do several regressions:
for(i in 2:4){
reg = lm(x ~ mydata[,i])
print(reg)
}
Call:
lm(formula = x ~ mydata[, i])
Coefficients:
(Intercept) mydata[, i]
1988.0088 -0.1341
Call:
lm(formula = x ~ mydata[, i])
Coefficients:
(Intercept) mydata[, i]
1987.87 2.07
Call:
lm(formula = x ~ mydata[, i])
Coefficients:
(Intercept) mydata[, i]
1987.304 -4.101