I am working with paneldata and wish to conduct a series of bivariate fixed effects regressions from a list of variables.
A small part of my dataset looks like this:
library(plm)
library(dplyr)
structure(list(v2x_libdem = c(0.876, 0.88, 0.763, 0.779), v2x_partipdem = c(0.679,
0.68, 0.647, 0.652), v2x_frassoc_thick = c(0.937, 0.937, 0.9,
0.899), country_name = c("Sweden", "Sweden", "Hungary", "Hungary"
), year = c(2000, 2008, 2000, 2008)), row.names = c(NA, -4L), class = c("tbl_df",
"tbl", "data.frame"))
# A tibble: 4 × 5
v2x_libdem v2x_partipdem v2x_frassoc_thick country_name year
<dbl> <dbl> <dbl> <chr> <dbl>
1 0.876 0.679 0.937 Sweden 2000
2 0.88 0.68 0.937 Sweden 2008
3 0.763 0.647 0.9 Hungary 2000
4 0.779 0.652 0.899 Hungary 2008
My list of variables looks something like this:
vars <- c("v2x_libdem", "v2x_partipdem", "v2x_frassoc_thick")
These variables ought to be both x and y, and thus every combination of the variables as x and y should be in a regression.
I have written a function that conducts the paneldata modelling:
paneldata_function <- function (y, x) { #this function will do a PLM
plm(paste(y, "~", x),
data = Vdem_west,
model = "within",
index = c("year", "country_name")) #for a given x & y variable
}
It is this function I need to loop over with each combination of x and y. Preferably, I would want the output to a be four vectors of values which I might turn into a dataset; one for the coefficient, one for the x variable, one for the y variable and one for the p-value.
If I do one single model, I can access these easily:
e <- paneldata_function("v2x_libdem", "v2x_partipdem")
p_value <- e[["fstatistic"]][["p.value"]]
x_var <- names(e[["model"]])[2]
y_var <- names(e[["model"]])[1]
How might I write the loop so that I get these vectors as an output?
Any help is very appreciated, and I hope I phrased my question as clearly as possible.