'For loop in R - Error: $ operator is invalid for atomic vectors
I am attempting to find the significance of factor exposures, from a 5x60 matrix of df_exposures
- 5 factors & 60 portfolios
The df_exposures
matrix was initially calculated by regressing the monthly returns on 60 portfolios on certain "macroeconomic shocks," - TM2R, IPR, InfR, UnR, OilR in the following loop
# Assign variable names
TM2R <- data[,2]
IPR <- data[,3]
InfR <- data[,4]
UnR <- data[,5]
OilR <- data[,6]
# Run regression
for (i in 7:69){
model_name <- paste0("model_", i, ".csv")
model <- lm(data = data, data[[i]]~TM2R+IPR+InfR+UnR+OilR)
#Model coefficients
tidy1 <- tidy(model)
#Standard Errors in regression results
tidy1$vcov <- vcovHC(model, type = "HC1")
write.csv(tidy1, file = model_name)
}
The loop which I am using to find how many of these exposures/"beta-hats," are significant is as follows
alpha <- 0.05
for (i in 1:ncol(df_exposure)){
for (j in 1:nrow(df_exposure)){
# Store Beta_hat of asset "i" to shock "j"
beta_hat <- model[[i]]$coefficients[[j+1]]
df_exposure[j,i] <- beta_hat
#Store beta_hat if significant in df_significance
pval <- coef(summary(model[[i]]))[j+1, "pr(>|t|)"]
if(pval > alpha){
df_significance[j,i] <- 0
}else{
df_significance[j,i] <- beta_hat
}
}
}
However, R returns the error Error: $ operator is invalid for atomic vectors
.
I am unable to find a way to convert model[[i]]
from an atomic vector to something else within a loop.
It may be important to note that R returns
is.atomic(model[[i]]) = TRUE
is.atomic(model) = FALSE
Any help on the matter would be appreciated, there may also be a way to find how many of the model coefficients are significant within the first loop?
Thanks :)
Solution 1:[1]
I answer myself in case it helps someone in the future. The metadata service doesn't seem to work well to overwrite the config from cloud-init although in the cloud-init logs it was seen that it recovered http://controller:8775/openstack/2016-10-06/network_data.json... Anyway, to get past this problem I just mounted the disk and directly edited ....etc/netplan/50-cloud-init.yaml with the expected MAC and then was binded properly.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |