使用r和winbug进行多变量测试

时间:2016-06-23 13:04:24

标签: r bayesian winbugs multivariate-testing

如何使用RWinBUGS14对多变量的平均值(ttest)进行区分? 我有一个多变量结果y和分类变量x。我可以使用下面的代码从多变量中获取MCMC采样值的均值,但是如何通过变量x测试均值的差异?

以下是R代码

library(R2WinBUGS)
library(MASS)  # need to mvrnorm
library(MCMCpack) # need for rwish
# Generate synthetic data
N <- 500
#we use this to simulate the data
S <- matrix(c(1,.2,.2,5),nrow=2)

#Produces one or more samples from the specified multivariate normal distribution.
#produces 2 variables with the given distribution
y <- mvrnorm(n=N,mu=c(1,3),Sigma=S)
x <- rbinom(500, 1, 0.5)

# Set up for WinBUGS
#set up of the mu0 values
mu0 <-  as.vector(c(0,0))


#covariance matrices
# the precisions
S2 <- matrix(c(1,0,0,1),nrow=2)/1000 #precision for unkown mu
# precison matrix to be passes to the wishart distribution for the tau
S3 <-  matrix(c(1,0,0,1),nrow=2)/10000 

#the data for the winbug code
data <- list("y","N","S2","S3","mu0")
inits <- function(){
                    list( mu=mvrnorm(1,mu0,matrix(c(10,0,0,10),nrow=2) ), 
                      tau <-  rwish(3,matrix(c(.02,0,0,.04),nrow=2)) )
                  }

# Run WinBUGS
bug_file <- paste0(getwd(), "/codes/mult_normal.bug")
multi_norm.sim <- bugs(data,inits,model.file=bug_file,
parameters=c("mu","tau"),n.chains = 2,n.iter=4010,n.burnin=10,n.thin=1,
bugs.directory="../WinBUGS14/",codaPkg=F)

print(multi_norm.sim,digits=3)

这是名为WinBUGS14

mult_normal.bug代码
model{
for(i in 1:N)
{
y[i,1:2] ~ dmnorm(mu[],tau[,])
}
mu[1:2] ~ dmnorm(mu0[],S2[,])
#parameters of a wishart
tau[1:2,1:2] ~  dwish(S3[,],3)
}

1 个答案:

答案 0 :(得分:1)

2个步骤:

  1. 使用样本统计信息加载函数来运行t.test,而不是直接执行。

    t.test2 <- function(m1,m2,s1,s2,n1,n2,m0=0,equal.variance=FALSE)
    {
       if( equal.variance==FALSE ) 
       {
         se <- sqrt( (s1^2/n1) + (s2^2/n2) )
         # welch-satterthwaite df
         df <- ( (s1^2/n1 + s2^2/n2)^2 )/( (s1^2/n1)^2/(n1-1) + (s2^2/n2)^2/(n2-1) )
       } else
       {
         # pooled standard deviation, scaled by the sample sizes
         se <- sqrt( (1/n1 + 1/n2) * ((n1-1)*s1^2 + (n2-1)*s2^2)/(n1+n2-2) ) 
         df <- n1+n2-2
       }      
       t <- (m1-m2-m0)/se 
       dat <- c(m1-m2, se, t, 2*pt(-abs(t),df))    
       names(dat) <- c("Difference of means", "Std Error", "t",      "p-value")
      return(dat) 
     }
    
  2. 解析我们要针对x测试的事物的均值和标准差,然后将它们传递给函数。

    mu1   <- as.data.frame(multi_norm.sim$mean)$mu[1]
    sdmu1 <- multi_norm.sim$sd$mu[1]
    t.test2( mean(x), as.numeric(mu1), s1 = sd(x), s2 = sdmu1, 500, 500)
    
  3.   

    平均值差异标准误差t p值

      -4.950656e-01        2.246905e-02       -2.203323e+01        5.862968e-76
    

    当我将屏幕上的结果复制到SO时,很难让结果的标签间隔开,我很抱歉。