楼主: xingzhaoh
6327 30

[问答] 关于R2WinBUGS数据的读入问题 [推广有奖]

11
xingzhaoh 发表于 2013-10-25 16:56:25
dongshengjay 发表于 2013-10-25 16:33
It's OK!
我试试
谢谢你

好像算起来时间会很长

12
xingzhaoh 发表于 2013-10-25 17:23:08
dongshengjay 发表于 2013-10-25 16:30
library(R2WinBUGS)
library(lattice)
library(coda)
现在mass是因变量,length是自变量,ngroups是区组,那pop是什么?是mass和length的总个数吗?

13
dongshengjay 发表于 2013-10-25 18:41:23
忘记告诉你了,winbugs很快就计算结束了,结束后需要关掉winbugs,r软件才能进行后续的运算

14
dongshengjay 发表于 2013-10-25 20:42:36
xingzhaoh 发表于 2013-10-25 17:23
现在mass是因变量,length是自变量,ngroups是区组,那pop是什么?是mass和length的总个数吗?
楼主应该搞清楚多水平模型的概念,pop再次应该是水平1的变量

15
suzhzh 发表于 2013-10-27 09:20:11
Thank you to noting me. But to be honest, it is a long time since I use WinBUGS, thus, I can't help you. Sorry for this.

16
xingzhaoh 发表于 2013-10-27 10:44:17
dongshengjay 发表于 2013-10-25 20:42
楼主应该搞清楚多水平模型的概念,pop再次应该是水平1的变量
大侠,你能帮我是试试吗?怎么把pop改成水平1的变量(mass是因变量,length是自变量,ngroups是区组)
数据改成什么样子才能算出来?这几天都没做出来,如果可以的话你用附件的数据试试,告诉我一下怎么做,现在头都大了,怎么都做不出来了。

17
xingzhaoh 发表于 2013-10-27 11:35:13
dongshengjay 发表于 2013-10-25 20:42
楼主应该搞清楚多水平模型的概念,pop再次应该是水平1的变量
出自Introduction to WinBUGS for Ecologists这本书的12章(麻烦您了,你费心看看怎么可以做出来,如果这个麻烦的话,你那有现成的例子让我用也可以,就是利用winbug算混合模型,截距、斜率、截距和斜率分别作为随机变量时怎么拟合出来,感觉他给的例子太麻烦,你那有现成的就更好了)
Chapter 12: Linear mixed-effects model
全部代码
  1. n.groups <- 56                                # Number of populations
  2. n.sample <- 10                                # Number of vipers in each pop
  3. n <- n.groups * n.sample                 # Total number of data points
  4. pop <- gl(n = n.groups, k = n.sample)         # Indicator for population

  5. # Body length (cm)
  6. original.length <- runif(n, 45, 70)
  7. mn <- mean(original.length)
  8. sd <- sd(original.length)
  9. cat("Mean and sd used to normalise.original length:", mn, sd, "\n\n")
  10. length <- (original.length - mn) / sd
  11. hist(length, col = "grey")

  12. Xmat <- model.matrix(~pop*length-1-length)
  13. print(Xmat[1:21,], dig = 2)                 # Print top 21 rows

  14. intercept.mean <- 230                        # mu_alpha
  15. intercept.sd <- 20                        # sigma_alpha
  16. slope.mean <- 60                        # mu_beta
  17. slope.sd <- 30                                # sigma_beta

  18. intercept.effects<-rnorm(n = n.groups, mean = intercept.mean, sd = intercept.sd)
  19. slope.effects <- rnorm(n = n.groups, mean = slope.mean, sd = slope.sd)
  20. all.effects <- c(intercept.effects, slope.effects) # Put them all together

  21. lin.pred <- Xmat[,] %*% all.effects        # Value of lin.predictor
  22. eps <- rnorm(n = n, mean = 0, sd = 30)        # residuals
  23. mass <- lin.pred + eps                        # response = lin.pred + residual

  24. hist(mass, col = "grey")                # Inspect what we’ve created

  25. library("lattice")
  26. xyplot(mass ~ length | pop)



  27. ### 12.3. Analysis under a random-intercepts model


  28. ### 12.3.1. REML analysis using R
  29. library('lme4')
  30. lme.fit1 <- lmer(mass ~ length + (1 | pop), REML = TRUE)
  31. lme.fit1


  32. ### 12.3.2. Bayesian analysis using WinBUGS
  33. # Write model
  34. sink("lme.model1.txt")
  35. cat("
  36. model {

  37. # Priors
  38. for (i in 1:ngroups){               
  39.     alpha[i] ~ dnorm(mu.int, tau.int)        # Random intercepts
  40. }

  41. mu.int ~ dnorm(0, 0.001)                # Mean hyperparameter for random intercepts
  42. tau.int <- 1 / (sigma.int * sigma.int)
  43. sigma.int ~ dunif(0, 100)                # SD hyperparameter for random intercepts

  44. beta ~ dnorm(0, 0.001)                        # Common slope
  45. tau <- 1 / ( sigma * sigma)        # Residual precision
  46. sigma ~ dunif(0, 100)                        # Residual standard deviation

  47. # Likelihood
  48. for (i in 1:n) {
  49.     mass[i] ~ dnorm(mu[i], tau)                # The random variable
  50.     mu[i] <- alpha[pop[i]] + beta* length[i] # Expectation
  51. }
  52. }
  53. ",fill=TRUE)
  54. sink()

  55. # Bundle data
  56. win.data <- list(mass = as.numeric(mass), pop = as.numeric(pop),
  57. length = length, ngroups = max(as.numeric(pop)), n = n)

  58. # Inits function
  59. inits <- function(){list(alpha = rnorm(n.groups, 0, 2), beta = rnorm(1, 1, 1),
  60. mu.int = rnorm(1, 0, 1), sigma.int = rlnorm(1), sigma = rlnorm(1))}

  61. # Parameters to estimate
  62. parameters <- c("alpha", "beta", "mu.int", "sigma.int", "sigma")

  63. # MCMC settings
  64. ni <- 2000
  65. nb <- 500
  66. nt <- 2
  67. nc <- 3

  68. # Start Gibbs sampling
  69. out <- bugs(win.data, inits, parameters, "lme.model1.txt", n.thin=nt,
  70. n.chains=nc, n.burnin=nb, n.iter=ni, debug = TRUE)

  71. # Inspect results
  72. print(out, dig = 3)
复制代码

18
dongshengjay 发表于 2013-10-28 16:10:30
截距及系数的随机效应模型,与单个截距效应模型相似,在指定先验后,将pop的脚标加上即可mu[i] <- alpha[pop[i]] + beta[pop[i]]* length[i] ,这本书的的p159-12.4.2的模型即可,并不复杂,你可以模仿
如果pop对应你的数据每个不同的分组。

19
xingzhaoh 发表于 2013-10-28 19:09:29
dongshengjay 发表于 2013-10-28 16:10
截距及系数的随机效应模型,与单个截距效应模型相似,在指定先验后,将pop的脚标加上即可mu
还是没这么明白,pop和group怎么设置,您能帮用我的data1 数据试试这个例子吗?我没看懂你的回复,基础太差,希望您可以用我的data,做出个成功的例子,教教我,谢谢您

20
dongshengjay 发表于 2013-10-28 20:15:27
library(R2WinBUGS)
library(lattice)
library(coda)
dat=read.csv("c:/data.csv")
attach(dat)
setwd("c:/")

sink("lme.model2.txt")
cat("
model {

# Priors
for (i in 1:6){               
    alpha[i] ~ dnorm(mu.int, tau.int)        # Random intercepts
    beta[i] ~ dnorm(mu.slope, tau.slope)# Random slopes
}

mu.int ~ dnorm(0, 0.001)                # Mean hyperparameter for random intercepts
tau.int <- 1 / (sigma.int * sigma.int)
sigma.int ~ dunif(0, 100)                # SD hyperparameter for random intercepts

mu.slope ~ dnorm(0, 0.001)                # Mean hyperparameter for random slopes
tau.slope <- 1 / (sigma.slope * sigma.slope)
sigma.slope ~ dunif(0, 100)                # SD hyperparameter for slopes

tau <- 1 / ( sigma * sigma)                # Residual precision
sigma ~ dunif(0, 100)                        # Residual standard deviation

# Likelihood
for (i in 1:n) {
    mass[i] ~ dnorm(mu[i], tau)
    mu[i] <- alpha[pop[i]] + beta[pop[i]]* length[i]
}
}
",fill=TRUE)
sink()

# Bundle data
win.data <- list(mass = as.numeric(mass), pop = pop,
length = length, n =length(mass))

# Inits function
inits <- function(){ list(alpha = rnorm(6, 0, 2), beta = rnorm(6, 10, 2),
mu.int = rnorm(1, 0, 1), sigma.int = rlnorm(1), mu.slope = rnorm(1, 0, 1),
sigma.slope = rlnorm(1), sigma = rlnorm(1))}

# Parameters to estimate
parameters <- c("alpha", "beta", "mu.int", "sigma.int", "mu.slope", "sigma.slope", "sigma")

# MCMC settings
ni <- 2000
nb <- 500
nt <- 2
nc <- 3

# Start Gibbs sampling
out <- bugs(win.data, inits, parameters, "lme.model2.txt", n.thin=nt,
n.chains=nc, n.burnin=nb, n.iter=ni, debug = TRUE)

print(out, dig = 2)
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
Nicolle + 1 + 1 + 1 精彩帖子

总评分: 学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-30 08:17