楼主: ynsxx
8152 18

[精品图书下载]Bayesian Methods for Data Analysis 3rd Edition 2008 [推广有奖]

11
flarehe(未真实交易用户) 发表于 2015-7-7 11:19:27
请问这本书下载如何获取论坛比?

12
jgchen1966(真实交易用户) 发表于 2015-12-7 23:54:36

13
ReneeBK(未真实交易用户) 发表于 2016-5-30 00:12:52
  1. postplot <- function(mu, tau, ybar, sigma, n){
  2.   denom <- sigma^2/n + tau^2
  3.   mu <- (sigma^2/n*mu + tau^2*ybar)/denom
  4.   stdev <- sqrt(sigma^2/n*tau^2/denom)
  5.   return(c(mu,stdev))
  6.   }

  7. x <- seq(-2,8,length.out=100)
  8. plot(x, dnorm(x, mean=2, sd=1), ylim=c(0, 1.3),
  9.   xlab=expression(theta), ylab="density", type= "l")

  10. param1 <- postplot(2,1,6,1,1)
  11. lines(x, dnorm(x, mean=param1[1], sd=param1[2]), lty=2)
  12. param10 <- postplot(2,1,6,1,10)
  13. lines(x, dnorm(x, mean=param10[1], sd=param10[2]), lty=3)
  14. legend(-2, 1.3, legend=c("prior", "posterior with n=1",
  15.   "posterior with n=10"), lty=1:3, ncol=1)

  16. y1 <- rnorm(2000, mean=param1[1], sd=param1[2])
  17. ## param1: [1] 4.0000000 0.7071068
  18. r1 <- hist(y1, freq=F, breaks=20, plot=F)
  19. lines(r1, lty=2, freq=F, col="gray90")
复制代码

14
ReneeBK(未真实交易用户) 发表于 2016-5-30 00:13:24
  1. alpha<-5
  2. beta<-6
  3. theta<-seq(0, 100, length.out=101)
  4. prior <- dgamma(theta, shape=alpha, scale=beta)
  5. x<-42
  6. posterior <- dgamma(theta, shape=x+alpha, scale=1/(1+1/beta) )

  7. plot(theta, posterior, xlab=expression(theta), ylab="density", type="l")
  8. lines(theta, prior, lty=3)

  9. postdraw <- rgamma(2000, shape=x+alpha, scale=1/(1+1/beta))
  10. r1<-hist(postdraw, freq=F, breaks=20, plot=F)
  11. lines(r1, lty=3, freq=F, col="gray90")
复制代码

15
ReneeBK(未真实交易用户) 发表于 2016-5-30 00:14:38
  1. par(mfrow=c(1,2))

  2. x <- seq(.01,.99,length=99);  top <- 3.25
  3. plot(x,dbeta(x,.5,.5),type="l",xlab=expression(theta),
  4.   ylab="prior density",ylim=c(0,top))
  5. lines(x,dbeta(x,1,1),lty=2)
  6. lines(x,dbeta(x,2,2),lty=3)
  7. legend(.2,top,cex=1.2,legend =c("Beta(.5,.5)  (Jeffreys prior)",
  8.   "Beta(1,1)  (uniform prior)",
  9.   "Beta(2,2)  (skeptical prior)"),lty=1:3)

  10. x <- seq(0,1,length=100);
  11. plot(x,dbeta(x,13.5,3.5),type="l",lty=1,xlab=expression(theta),
  12.   ylab="posterior density")
  13. lines(x,dbeta(x,14,4),lty=2)
  14. lines(x,dbeta(x,15,5),lty=3)
  15. legend(0.1 ,3.5,cex=1.2,legend =c(
  16. "Beta(13.5,3.5)", "Beta(14,4)", "Beta(15,5)"),lty=1:3)

  17. a <- 0.025 ; b <- 0.5; d <- 0.975
  18. a1 <- qbeta(a,13.5,3.5); a2 <- qbeta(b,13.5,3.5)
  19. a3 <- qbeta(d,13.5,3.5); a4 <- pbeta(0.6,13.5,3.5); a5 <- 1-a4
  20. b1 <- qbeta(a,14,4); b2 <- qbeta(b,14,4)
  21. b3 <- qbeta(d,14,4); b4 <- pbeta(0.6,14,4); b5 <- 1-b4
  22. e1 <- qbeta(a,15,5); e2 <- qbeta(b,15,5)
  23. e3 <- qbeta(d,15,5); e4 <- pbeta(0.6,15,5); e5 <- 1-e4

  24. cat("q_.025    q_.50     q_.975    P(theta >.6)\n")
  25. prob <- c( a1,a2,a3,a5,b1,b2,b3,b5,e1,e2,e3,e5)
  26. p <- t(matrix(prob,4,3))
  27. print(round(p,3))
复制代码

16
ReneeBK(未真实交易用户) 发表于 2016-5-30 00:15:56
  1. ###########################
  2. #  MAC fraility example -- R code to arrange the dataset
  3. #    Weibull survival model -- Example 2.15
  4. ###########################


  5. # Our dataset is unbalanced, as the number of subjects differs from site to site:
  6. # nsub=c(4,2,1,10,8,12,6,6,13,4,3), i.e. there are 4 subjects in site 1, 2 in site 2, etc.
  7. #
  8. # We use "offset" to hold the position in the dataset at which each person's data starts.
  9. # Here is a snippet of R code to calculate offset from nsub:

  10.     nsub<-c(4,2,1,10,8,12,6,6,13,4,3)
  11.     temp<-1
  12.     offset<-rep(0,(length(nsub)+1))
  13.     for(i in 1:length(nsub)){offset[i]<-temp; temp<-temp+nsub[i]}
  14.     offset[ (length(nsub)+1)] <-sum(nsub)+1
复制代码

17
ReneeBK(未真实交易用户) 发表于 2016-5-30 00:16:41
  1. ####################
  2. #   R CODE TO ILLUSTRATE THE BAYESIAN CENTRAL LIMIT THEOREM
  3. #    USING BETA/BINOMIAL (HAMBURGER PATTIES) EXAMPLE
  4. ####################

  5. x <- seq(0,1,length=100);
  6. plot(x,dbeta(x,14,4),type="l",lty=1,xlab=expression(theta),
  7.   ylab="posterior density")
  8. phat <- 13/16;  var <- phat*(1-phat)/16
  9. lines(x,dnorm(x,phat,sqrt(var)),lty=2)
  10. legend(0 ,3.5,cex=1.2,legend =c(
  11. "exact (beta)", "approximate (normal)"),lty=1:2)
复制代码

18
ReneeBK(未真实交易用户) 发表于 2016-5-30 00:17:45
  1. ###############
  2. #  R code for 2-stage Bayesian GLM (Lindley and Smith, 1972)
  3. #  See Example 4.1 in Carlin and Louis (2008)
  4. ###############

  5. X <-cbind(rep(1,n),lgage)
  6. mu <- c(0,0)
  7. R <- matrix(c(0.001,0,0,0.001), nrow=2)
  8. tau <- c(1,100)

  9. postfn <- function(Y, X, mu, R, tau){
  10.   p <- dim(X)[2]
  11.   D <- solve( tau*t(X)%*%X + R)
  12.   d <- tau*t(X)%*%Y + R%*%diag(rep(1,p))%*%mu
  13.   postmean <- D%*%d
  14.   postsd <- sqrt(diag(D))
  15.   return(list(mean= D%*%d, sd=sqrt(diag(D)) ))
  16.   }

  17. post1 <- postfn(Y, X, mu, R, tau[1]) # posterior for tau=1
  18. beta <- post1$mean[2]
  19. postsd <- post1$sd[2]
  20. CI1 <- c(beta+qnorm(0.025)*postsd , beta+qnorm(0.975)*postsd)

  21. post2 <- postfn(Y, X, mu, R, tau[2]) # posterior for tau=100
  22. beta <- post1$mean[2]
  23. postsd <- post1$sd[2]
  24. CI2 <- c(beta+qnorm(0.025)*postsd , beta+qnorm(0.975)*postsd)

  25. theta <- seq(-0.2,0.6,length.out=100)
  26. dens1<-dnorm(theta, mean=post1$mean[2], sd=post1$sd[2] )
  27. dens2<-dnorm(theta, mean=post2$mean[2], sd=post2$sd[2] )
  28. plot(theta, dens1, xlab=expression(beta), xlim=c(-0.2, 0.6),
  29.   ylim=c(0,20), ylab="posterior density", type="n")
  30. lines(theta, dens1, lty=1)        # posterior density for tau=1
  31. lines(theta, dens2, lty=2)        # posterior density for tau=100

  32. postdraw <- rnorm(2000, mean=post2$mean[2], sd=post2$sd[2] )
  33. r1<-hist(postdraw,freq=F,breaks=20, plot=F)
  34. lines(r1,lty=3, freq=F, col="gray90")   #posterior draws, tau=100
  35. legend(-0.2, 20, legend=c(expression(paste(tau,"=1", sep="")),
  36.   expression(paste(tau,"=100", sep=""))), lty=c(1,2), ncol=1)
  37.   
复制代码

19
胆酸止咳片9(未真实交易用户) 发表于 2018-10-9 21:51:51
2100

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

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-7 22:21