Reputation: 31
I have a df that looks like this:
Country Year X Y1 Y2 Y3 Y4
ARG 1990 0.4875 1.23 2.51 1.42 4.85
ARG 1991 0.4952 1.66 2.31 1.25 5.02
ARG 1992 0.5120 1.87 2.57 1.92 4.66
ARG 1993 0.5213 1.96 2.41 2.42 5.85
Where X is my independent variable and Ys are the dependent variables I want to loop on.
I tried something like:
for (i in out_start:out_end){
outcome = colnames(RegTest)[i]
model <- plm(get(outcome) ~ X, data=RegTest, index=c("Country", "year"), model= "within")
summary(model)
}
Where I previously set out_start at 4 and end at the end of my columns but I get an error message saying:
Error in uniqval[as.character(effect), , drop = F] :
incorrect number of dimensions
I can't see what is wrong since it seems to get correctly the name of the column.
PS: if someone knows also how to list all summaries in one df it would be super helpful.
Upvotes: 0
Views: 1000
Reputation: 1508
Load the following packages
library(tidyverse)
library(plm)
library(broom)
Let's creat your data set: beware, I think the error message depends on "Country" variable which needs more than one value to run the model.
RegTest <- data.frame(rep(c("ARG","SPA"),2),
c(1990,1990,1991,1991),
c(0.4875,0.4952,0.5120,0.5213),
c(1.23,1.66,1.87,1.96),
c(2.51,2.31,2.57,2.41),
c(1.42,1.25,1.92,2.42),
c(4.85,5.02,4.66,5.85))
names(RegTest) <- c("Country","Year","X","Y1","Y2","Y3","Y4")
Pick up the indipendent variables from the original dataset
indipendent_variables <- RegTest[,4:7]
Build a function with your model, setting indipendet_variables
like the main argument of the function
plm_function <- function(indipendent_variable)
plm(indipendent_variables ~ X, data=RegTest, index=c("Country","Year"), model="within")
Let's use map
function from purrr package to run iteratively your function without using for loops
models <- map(indipendent_variables, plm_function)
Inspect model results using tidy
function from broom package
tidy(models$Y1)
To get more statistics from model results use augment
function from broom package
tidy(augment(models$Y1))
Save augmented results in a dataframe
results_Y1 <- tidy(augment(models$Y1))
Upvotes: 1
Reputation: 9668
For solving the issue with the formula You could use the paste()
function in conjunction with as.formula()
.
It is fairly easy to save the results produced by each call of summary()
in a list. You could initialize model
as a list
before the loop and assign the outcome from each iteration of the loop as an entry to model
.
The following code chunk demonstrates how to do this using the dataset Fatalities
from package AER
. I chose miles
as the regressor and the loop runs over several measures of traffic fatalities.
library(AER)
library(plm)
data("Fatalities")
out_start <- 17
out_end <- 26
model <- list()
for (i in out_start:out_end){
outcome <- colnames(Fatalities)[i]
model[[i-out_start+1]] <- summary(
plm(as.formula(paste(outcome, " ~ miles", sep = "")),
data = Fatalities,
index=c("state", "year"), model= "within")
)
}
model
Upvotes: 1