I am simulating a model for different values of the parameters. Essentially I obtain a list object results
containing several dataframes.
library(MASS)
model_beta <- 0
model_se <- 0
model_p <- 0
set.seed(seed = 42224)
sim <- function(S, T, rho1, rho2, sigma_u, sigma_v) {
Sigma <- matrix(c(sigma_u, 0, 0, sigma_v), 2, 2)
uv <- data.frame(mvrnorm(n = S * T,
mu = c(0, 0),
Sigma = Sigma))
for (s in 1:S){
x_t <- 0 + uv[(s - 1) * T + 1, 2]
y_t <- 0 + uv[(s - 1) * T + 1, 1]
for(i in 2:T) {
x_t[i] <- rho2 * x_t[i - 1] + uv[(s - 1) * T + i, 2]
y_t[i] <- rho1 * y_t[i - 1] + uv[(s - 1) * T + i, 1]
}
data <- data.frame(y_t, x_t)
model <- lm(y_t ~ 1 + x_t, data)
model_beta[s] <- model$coefficients[2]
model_se[s] <- summary(model)$coefficients[2, 2]
model_p[s] <- summary(model)$coefficients[, "Pr(>|t|)"][2]
rm(x_t, y_t, data)
all_ests <- as.data.frame(cbind(model_beta, model_se, model_p))
}
return(all_ests)
}
rho1_values <- c(0.1, 0.95, 0.99)
rho2_values <- c(0.1, 0.95, 0.99)
T_values <- c (100, 500, 1000)
results <- list()
for (rho1 in rho1_values) {
for (rho2 in rho2_values) {
for (T in T_values) {
result <- sim(S = 100, T, rho1, rho2, sigma_u = 1, sigma_v = 1)
results[[paste("rho1", rho1, "rho2", rho2, "T", T)]] <- result
}
}
}
I want to loop through each dataframe in the list object results
and perform an operation, averaging the values in it. But results$key$model_beta
does not seem to work. I would like to know how to do this loop as well as be able to call the variable I need. Any help is appreciated.
# recover beta mean and se estimates
mean_betas <- list()
sd_betas <- list()
p_betas <- list()
for (key in names(results)) {
beta <- mean(results$key$model_beta)
sd <- sd(results$key$model_beta)
ttest <- sum(results$key$model_p <= 0.05)
mean_betas[[key]] <- beta
sd_betas[[key]] <- sd
p_betas[[key]] <- ttest
}
Write a small summary function and put it in sapply
.
> fn <- \(x) c(b=mean(x$model_beta), SE=sd(x$model_beta), tt=sum(x$model_p <= .05))
> t(sapply(results, fn))
b SE tt
rho1 0.1 rho2 0.1 T 100 2.149949e-03 0.101009530 4
rho1 0.1 rho2 0.1 T 500 -5.390908e-03 0.046816577 7
rho1 0.1 rho2 0.1 T 1000 -2.366675e-03 0.030186145 5
rho1 0.1 rho2 0.95 T 100 2.558207e-03 0.050902888 6
rho1 0.1 rho2 0.95 T 500 -3.165286e-04 0.018866196 11
rho1 0.1 rho2 0.95 T 1000 1.608793e-04 0.011022998 7
rho1 0.1 rho2 0.99 T 100 2.443499e-03 0.037830310 7
rho1 0.1 rho2 0.99 T 500 -7.282507e-05 0.010791572 6
rho1 0.1 rho2 0.99 T 1000 -1.502523e-03 0.005661447 10
rho1 0.95 rho2 0.1 T 100 -4.565932e-03 0.288954389 12
rho1 0.95 rho2 0.1 T 500 -1.133511e-02 0.134098385 3
rho1 0.95 rho2 0.1 T 1000 8.865955e-03 0.109455782 6
rho1 0.95 rho2 0.95 T 100 -4.527339e-04 0.360571654 60
rho1 0.95 rho2 0.95 T 500 7.518125e-03 0.186272942 61
rho1 0.95 rho2 0.95 T 1000 6.602277e-03 0.140099632 62
rho1 0.95 rho2 0.99 T 100 7.203229e-02 0.346791084 66
rho1 0.95 rho2 0.99 T 500 3.040713e-03 0.151638473 81
rho1 0.95 rho2 0.99 T 1000 1.301691e-03 0.094071457 70
rho1 0.99 rho2 0.1 T 100 -3.140329e-02 0.351619905 7
rho1 0.99 rho2 0.1 T 500 2.359513e-02 0.253493421 7
rho1 0.99 rho2 0.1 T 1000 -6.793740e-02 0.203633365 6
rho1 0.99 rho2 0.95 T 100 -2.576093e-02 0.570548483 67
rho1 0.99 rho2 0.95 T 500 -2.155015e-02 0.483091736 74
rho1 0.99 rho2 0.95 T 1000 -6.842498e-03 0.369940130 82
rho1 0.99 rho2 0.99 T 100 -5.356651e-02 0.527453321 75
rho1 0.99 rho2 0.99 T 500 -5.667934e-02 0.335362430 82
rho1 0.99 rho2 0.99 T 1000 -4.967689e-02 0.283655774 84