'How to create a matrix with for loop using qgcomp() in R
I always use a for loop to create a matrix of linear and logistic regression output, but I am having difficulty doing so with qgcomp(). If anyone has experience or advice, I would greatly appreciate it. I prefer this approach over functions since all it takes is 1 click. The outputted matrix only contains "NA" and even when I remove the try catch error, it gives me 1 of 2 erros: Error: $ operator is invalid for atomic vectors or Error in x[good, , drop = FALSE] : (subscript) logical subscript too long. I have tried every single possible iteration with the quotation marks within the model statement... and also to use the typical cbind() trick for multivariate regressions.
Here is an example:
mtcars
IVs <- c("mpg", "cyl", "vs")
mixture <- c("disp", "wt")
DVs <- c("drat", "qsec")
matrix <- matrix(nrow = 2, ncol = 5)
colnames(matrix) <- c("Psi_beta", "Psi.LCL", "Psi.UCL", "Pos.psi.weights", "Neg.psi.weights")
rownames(matrix) <- DVs
for(i in 1:2){
tryCatch(
{
mod1 <- qgcomp.noboot(paste0(DVs[i],"~mpg", "+factor(cyl)", "+factor(vs)"
,"+disp","+wt"),
dat=mtcars,
expnms=mixture,
q=4,
family=gaussian())
coef <- as.data.frame(summary(mod1))
pospsi <- as.data.frame(mod1$pos.psi)
negpsi <- as.data.frame(mod1$neg.psi)
matrix[i,1] <- round(coef[2,1], 3) # extract overall mixture estimate
matrix[i,2] <- round(coef[2,3], 3) #extract LCL for mixture
matrix[i,3] <- round(coef[2,4], 3) #extract LCL for mixture
matrix[i,4] <- round(pospsi[1], 3) #extract sum of positive weights for mixture
matrix[i,5] <- round(negpsi[1], 3) #extract sum of negative weights for mixture
}, error=function(e){})
}
Solution 1:[1]
It's hard to tell what is happening when I can't see what mix.df looks like. Given the error you are facing, it is likely that you are trying to access a vector using $
notation. As far as I can tell, qgcomp.boot returns a list, so that is not likely to be the source of your error. Therefore, I think it might have something to do with mix.df.
Edit: I've made the code and I think it does what you want it to. Take a look and let me know
mtcars
IVs <- c("mpg", "cyl", "vs")
mixture <- c("disp", "wt")
DVs <- c("drat", "qsec")
matrix <- matrix(nrow = 2, ncol = 5)
colnames(matrix) <- c("Psi_beta", "Psi.LCL", "Psi.UCL", "Pos.psi.weights", "Neg.psi.weights")
rownames(matrix) <- DVs
for(i in 1:2){
#Need to convert the string to a formula because qgcomp doesn't do that for us
mod1.formula <- as.formula(paste0(DVs[i],"~mpg", "+factor(cyl)", "+factor(vs)"
,"+disp","+wt"))
#Run qgcomp as usual, with the previous formula
mod1 <- qgcomp.noboot(mod1.formula,
dat=mtcars,
expnms=mixture,
q=4,
family=gaussian())
coef <- as.data.frame(summary(mod1))
pospsi <- as.data.frame(mod1$pos.psi)
negpsi <- as.data.frame(mod1$neg.psi)
matrix[i,1] <- round(coef[2,1], 3) # extract overall mixture estimate
matrix[i,2] <- round(coef[2,3], 3) #extract LCL for mixture
matrix[i,3] <- round(coef[2,4], 3) #extract LCL for mixture
#I changed the way you access popspsi and negpsi because
#they are dataframes and need to be treated differently
matrix[i,4] <- round(pospsi[1,1], 3) #extract sum of positive weights for mixture
matrix[i,5] <- round(negpsi[1,1], 3) #extract sum of negative weights for mixture
}
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 |