我正在尝试自动分析R中的一些数据表。
我有这样的表:
Theta1 Theta2 Theta3 Theta4 Sigma1 Sigma2 Omega1 Omega2 Omega3 Omega4
但是,T和O的数量因分析而异。因此,我想制作代码,以便R自动计算出有多少theta,sigma和omegas,并将它们放在自己的矩阵中进行单独分析。我在想某种正则表达式?但是我更熟悉STATA的命令。
我认为我很接近,但到目前为止这是我的代码:
mcmcChain <- as.matrix(MCMC) #switch MCMC object to matrix
mcmcNames <- colnames(mcmcChain) #make vector with column names
mcmcNames
[1] "THETA1" "THETA2" "THETA3" "THETA4" "THETA5" "SIGMA.1.1"
[7] "SIGMA.2.1" "SIGMA.2.2" "OMEGA.1.1" "OMEGA.2.1" "OMEGA.2.2" "OMEGA.3.1"
[13] "OMEGA.3.2" "OMEGA.3.3" "OMEGA.4.1" "OMEGA.4.2" "OMEGA.4.3" "OMEGA.4.4"
[19] "OMEGA.5.1" "OMEGA.5.2" "OMEGA.5.3" "OMEGA.5.4" "OMEGA.5.5" "MCMCOBJ"
nVar <- length(mcmcNames) #number of variables
for (i in 1:nVar) {
thetaNames <- mcmcNames[i] # !!some way to identify only theta's!!
}
nTheta <- length(thetaNames) #n theta variables
thetaSamp <- NULL
for (j in 1:nTheta) { #cbind all thetas to one column
thetaSamp <- cbind(thetaSamp,
mcmcChain[,paste("THETA",j,sep="") ] )
}
感谢您的帮助!
答案 0 :(得分:1)
thetadata<-data.frame(mcmcChain[,grep("^THETA",colnames(mcmcChain))]
答案 1 :(得分:1)
这将在名称中将所有列与“THETA”隔离,并将它们存储在名为“thetas”的自己的矩阵中:
thetas <- mcmcChain[, c(grep("THETA", colnames(mcmcChain)))]