0

我正在尝试自动分析 R 中的某些数据表。

我有这样的表:

Theta1 Theta2 Theta3 Theta4 Sigma1 Sigma2 Omega1 Omega2 Omega3 Omega4

但是,T 和 O 的数量因分析而异。所以我想编写代码,以便 R 自动计算出有多少 theta、sigma 和 omega,并将它们放入自己的矩阵中以分别分析。我在想某种正则表达式?但更熟悉 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="") ] )
}

感谢您的任何帮助!

4

2 回答 2

1
thetadata<-data.frame(mcmcChain[,grep("^THETA",colnames(mcmcChain))]
于 2013-11-08T20:39:08.467 回答
1

这将隔离名称中带有“THETA”的所有列,并将它们存储在自己的称为“thetas”的矩阵中:

thetas <- mcmcChain[, c(grep("THETA", colnames(mcmcChain)))]
于 2013-11-08T20:39:33.933 回答