楼主: Lisrelchen
3559 13

[贝叶斯必读]Bayesian Cognitive Modelling(using R) [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6306
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2014-11-30 00:28:46 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币

  • http://www.amazon.com/Bayesian-Cognitive-Modeling-Practical-Course/dp/1107018455/ref=sr_1_1?ie=UTF8&qid=1369156307&sr=8-1&keywords=bayesian+cognitive+modeling+a+practical+course
  • http://bayesmodels.com/

二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Modelling Cognitive Bayesian modelli modell

已有 2 人评分论坛币 学术水平 热心指数 信用等级 收起 理由
日新少年 + 2 + 2 + 2 精彩帖子
crystal8832 + 48 + 2 + 2 + 2 奖励积极上传好的资料

总评分: 论坛币 + 48  学术水平 + 4  热心指数 + 4  信用等级 + 4   查看全部评分

本帖被以下文库推荐

沙发
Lisrelchen 发表于 2014-11-30 00:29:17
  1. # R2WinBUGS Example;
  2. # When you work through the code for the first time,
  3. # execute each command one at a time to better understand
  4. # what it does.

  5. # clears workspace:  
  6. rm(list=ls())

  7. # sets working directories:
  8. setwd("C:/Users/EJ/Dropbox/EJ/temp/BayesBook/test/ParameterEstimation/Binomial")

  9. library(R2WinBUGS)
  10. bugsdir <- "C:/Program Files/WinBUGS14"

  11. k <- 5
  12. n <- 10

  13. data <- list("k", "n") # to be passed on to WinBUGS

  14. myinits <- list(
  15.   list(theta = 0.1), #chain 1 starting value
  16.   list(theta = 0.9)) #chain 2 starting value

  17. # parameters to be monitored:       
  18. parameters <- c("theta")

  19. # The following command calls WinBUGS with specific options.
  20. # For a detailed description see Sturtz, Ligges, & Gelman (2005).
  21. samples <- bugs(data, inits=myinits, parameters,
  22.                                  model.file ="Rate_1.txt",
  23.                                  n.chains=2, n.iter=20000, n.burnin=1, n.thin=1,
  24.                                  DIC=T, bugs.directory=bugsdir,
  25.                                  codaPkg=F, debug=F)
  26. # Now the values for the monitored parameters are in the "samples" object,
  27. # ready for inspection.

  28. # The commands below are useful for a quick overview:
  29. print(samples)  # a rough summary
  30. plot(samples)   # a visual representation
  31. names(samples)  # summarizes the variables
  32. samples$summary # more detailed summary
  33. chain <- 1
  34. samples$sims.array[1:15,chain,]# array: element, chain, column (theta/deviance)

  35. # Collect posterior samples across all chains:
  36. theta <- samples$sims.list$theta

  37. # Now let's plot a histogram for theta.
  38. # First, some options to make the plot look better:
  39. par(cex.main = 1.5, mar = c(5, 6, 4, 5) + 0.1, mgp = c(3.5, 1, 0), cex.lab = 1.5,
  40.     font.lab = 2, cex.axis = 1.3, bty = "n", las=1)
  41. Nbreaks <- 80
  42. y       <- hist(theta, Nbreaks, plot=F)
  43. plot(c(y$breaks, max(y$breaks)), c(0,y$density,0), type="S", lwd=2, lty=1,
  44.      xlim=c(0,1), ylim=c(0,10), xlab="Rate", ylab="Posterior Density")
  45. # NB. ylim=c(0,10) defines the range of the y-axis. Adjust the upper value
  46. # in case your posterior distribution falls partly outside this range.
复制代码

藤椅
Lisrelchen 发表于 2014-11-30 00:31:56
  1. # clears workspace:  
  2. rm(list=ls())

  3. # sets working directories:
  4. setwd("C:/Users/EJ/Dropbox/EJ/temp/BayesBook/test/ParameterEstimation/Binomial")

  5. library(R2WinBUGS)
  6. bugsdir <- "C:/Program Files/WinBUGS14"

  7. k1 <- 5
  8. k2 <- 7
  9. n1 <- 10
  10. n2 <- 10

  11. data <- list("k1", "k2", "n1", "n2") # to be passed on to WinBUGS
  12. myinits <-        list(
  13.   list(theta1 = 0.1, theta2 = 0.9))

  14. # parameters to be monitored:       
  15. parameters <- c("delta", "theta1", "theta2")

  16. # The following command calls WinBUGS with specific options.
  17. # For a detailed description see Sturtz, Ligges, & Gelman (2005).
  18. samples <- bugs(data, inits=myinits, parameters,
  19.                                  model.file ="Rate_2.txt",
  20.                                  n.chains=1, n.iter=10000, n.burnin=1, n.thin=1,
  21.                                  DIC=T, bugs.directory=bugsdir,
  22.                                  codaPkg=F, debug=F)
  23. # Now the values for the monitored parameters are in the "samples" object,
  24. # ready for inspection.

  25. # Collect posterior samples:
  26. delta <- samples$sims.list$delta

  27. # Now let's plot a histogram for delta.
  28. # First, some options to make the plot look better:
  29. par(cex.main = 1.5, mar = c(5, 6, 4, 5) + 0.1, mgp = c(3.5, 1, 0), cex.lab = 1.5,
  30.     font.lab = 2, cex.axis = 1.3, bty = "n", las=1)
  31. Nbreaks <- 80
  32. y       <- hist(delta, Nbreaks, plot=F)
  33. plot(c(y$breaks, max(y$breaks)), c(0,y$density,0), type="S", lwd=2, lty=1,
  34.      xlim=c(-1,1), ylim=c(0,10), xlab="Difference in Rates", ylab="Posterior Density")

  35. # mean of delta:
  36. mean(delta)
  37. # median of delta:
  38. median(delta)
  39. # mode of delta, estimated from the "density" smoother:
  40. density(delta)$x[which(density(delta)$y==max(density(delta)$y))]
  41. # 95% credible interval for delta:
  42. quantile(delta, c(.025,.975))
复制代码


板凳
Lisrelchen 发表于 2014-11-30 00:32:59
  1. # clears workspace:  
  2. rm(list=ls())

  3. # sets working directories:
  4. setwd("C:/Users/EJ/Dropbox/EJ/temp/BayesBook/test/ParameterEstimation/Binomial")

  5. library(R2WinBUGS)
  6. bugsdir <- "C:/Program Files/WinBUGS14"

  7. k1 <- 5
  8. k2 <- 7
  9. n1 <- 10
  10. n2 <- 10

  11. data <- list("k1", "k2", "n1", "n2") # to be passed on to WinBUGS
  12. myinits <-        list(
  13.   list(theta = 0.5))

  14. # parameters to be monitored:       
  15. parameters <- c("theta")

  16. # The following command calls WinBUGS with specific options.
  17. # For a detailed description see Sturtz, Ligges, & Gelman (2005).
  18. samples <- bugs(data, inits=myinits, parameters,
  19.                                  model.file ="Rate_3.txt",
  20.                                  n.chains=1, n.iter=1000, n.burnin=1, n.thin=1,
  21.                                  DIC=T, bugs.directory=bugsdir,
  22.                                  codaPkg=F, debug=T)
  23. # NB. This is the first time we use debug=T. It is often useful to study the
  24. # WinBUGS output before returning to R. When you are do exploring the chains
  25. # in WinBUGS, you have to close (not minimize) WinBUGS in order to return to R.

  26. theta <- samples$sims.list$theta
  27.                                 
  28. # Now let's plot a histogram for theta.
  29. # First, some options to make the plot look better:
  30. par(cex.main = 1.5, mar = c(5, 6, 4, 5) + 0.1, mgp = c(3.5, 1, 0), cex.lab = 1.5,
  31.     font.lab = 2, cex.axis = 1.3, bty = "n", las=1)
  32. Nbreaks <- 80
  33. y       <- hist(theta, Nbreaks, plot=F)
  34. plot(c(y$breaks, max(y$breaks)), c(0,y$density,0), type="S", lwd=2, lty=1,
  35.      xlim=c(0,1), ylim=c(0,10), xlab="Rate", ylab="Posterior Density")
复制代码


报纸
Lisrelchen 发表于 2014-11-30 00:33:34
  1. # clears workspace:  
  2. rm(list=ls())

  3. # sets working directories:
  4. setwd("C:/Users/EJ/Dropbox/EJ/temp/BayesBook/test/ParameterEstimation/Binomial")

  5. library(R2WinBUGS)
  6. bugsdir <- "C:/Program Files/WinBUGS14"

  7. k <- 1
  8. n <- 15
  9. # Uncomment for Trompetter Data
  10. # k <- 24
  11. # n <- 121

  12. data <- list("k", "n") # to be passed on to WinBUGS
  13. myinits <-        list(
  14.   list(theta = 0.5, thetaprior = 0.5))

  15. # parameters to be monitored:       
  16. parameters <- c("theta", "thetaprior", "postpredk", "priorpredk")

  17. # The following command calls WinBUGS with specific options.
  18. # For a detailed description see Sturtz, Ligges, & Gelman (2005).
  19. samples <- bugs(data, inits=myinits, parameters,
  20.                                  model.file ="Rate_4.txt",
  21.                                  n.chains=1, n.iter=5000, n.burnin=1, n.thin=1,
  22.                                  DIC=T, bugs.directory=bugsdir,
  23.                                  codaPkg=F, debug=T)
  24. # Now the values for the monitored parameters are in the "samples" object,
  25. # ready for inspection.

  26. ######################Plots########################################################################
  27. theta      <- samples$sims.list$theta
  28. thetaprior <- samples$sims.list$thetaprior
  29. postpredk  <- samples$sims.list$postpredk
  30. priorpredk <- samples$sims.list$priorpredk

  31. layout(matrix(c(1,2),2,1))
  32. layout.show(2)
  33. #Prior and posterior of theta
  34. plot(density(theta, from=0, to=1), zero.line=F, axes=F, main="", xlab="", ylab="", xlim=c(0,1), ylim=c(0,6))
  35. axis(1, at=c(0,0.2,0.4,0.6,0.8,1), lab=c("0","0.2","0.4","0.6","0.8","1"),cex.axis=0.8)
  36. mtext("Rate", side=1, line=2.25, cex=1.2)
  37. axis(2, at=c(0,2,4,6),cex.axis=0.8)
  38. mtext("Density", side=2, line=2.25, cex=1.2)
  39. lines(density(thetaprior, from=0, to=1), lty=3, col="gray")
  40. legend(0.6,5.75, c("Prior", "Posterior"), lty=c(3,1), col=c ("grey", "black"))

  41. #Prior and posterior predictive
  42. mybreaks <- seq(from=-.5,to=n+1,by=1)
  43. my.at    <- seq(from=0,to=n,by=1)
  44. hist(postpredk,breaks=mybreaks,freq=F, right=F, ylab="", xlab="", ylim=c(0,0.3),main="", axes=F )
  45. axis(1, at=my.at,lab=my.at,cex.axis=0.8)
  46. mtext("Success Count", side=1, line=2.25, cex=1.2)
  47. axis(2,at=c(0,0.1,0.2,0.3),lab=c("0","0.1","0.2","0.3"),cex.axis=0.8)
  48. mtext("Mass", side=2, line=2.25, cex=1.2)
  49. hist(priorpredk, breaks=mybreaks,freq=F,right=F,add=T, lty=3,border="grey")
  50. legend(8,0.3, c("Prior", "Posterior"), lty=c(3,1),col=c("grey", "black"))
复制代码


地板
Lisrelchen 发表于 2014-11-30 00:34:30
  1. # clears workspace:  
  2. rm(list=ls())

  3. # sets working directories:
  4. setwd("C:/Users/EJ/Dropbox/EJ/temp/BayesBook/test/ParameterEstimation/Binomial")

  5. library(R2WinBUGS)
  6. bugsdir <- "C:/Program Files/WinBUGS14"

  7. k1 <- 0
  8. k2 <- 10
  9. n1 <- 10
  10. n2 <- 10

  11. data <- list("k1", "k2", "n1", "n2") # to be passed on to WinBUGS
  12. myinits <-        list(
  13.   list(theta = 0.5))

  14. # parameters to be monitored:       
  15. parameters <- c("theta", "postpredk1", "postpredk2")

  16. # The following command calls WinBUGS with specific options.
  17. # For a detailed description see Sturtz, Ligges, & Gelman (2005).
  18. samples <- bugs(data, inits=myinits, parameters,
  19.                                  model.file ="Rate_5.txt",
  20.                                  n.chains=1, n.iter=1000, n.burnin=1, n.thin=1,
  21.                                  DIC=T, bugs.directory=bugsdir,
  22.                                  codaPkg=F, debug=T)

  23. theta      <- samples$sims.list$theta
  24. postpredk1 <- samples$sims.list$postpredk1
  25. postpredk2 <- samples$sims.list$postpredk2
  26.                                                                   
  27. # Two-panel plot.
  28. layout(matrix(c(1,2),1,2))
  29. layout.show(2)
  30. # First, a histogram for theta.
  31. par(cex.main = 1.5, mar = c(5, 6, 4, 5) + 0.1, mgp = c(3.5, 1, 0), cex.lab = 1.5,
  32.     font.lab = 2, cex.axis = 1.3, bty = "n", las=1)
  33. Nbreaks <- 80
  34. y       <- hist(theta, Nbreaks, plot=F)
  35. plot(c(y$breaks, max(y$breaks)), c(0,y$density,0), type="S", lwd=2, lty=1,
  36.      xlim=c(0,1), ylim=c(0,10), xlab="Theta", ylab="Density")
  37. # let's plot a density estimate over this:
  38. lines(density(theta), col="red", lwd=2)

  39. # Second plot, the data space (predictives)
  40. plot(k1,k2,type="p", pch=4, cex=2, lwd=2, xlab="Success Count 1", ylab="Success Count 2",
  41.      xlim=c(-1, n1+1), ylim=c(-1,n2+1))        
  42. nsamples <- length(theta)
  43. sc <- 10
  44. for (i in 0:n1)
  45. {
  46.   for (j in 0:n2)
  47.   {
  48.     match.preds <- sum(postpredk1==i & postpredk2==j)/nsamples
  49.     if (match.preds > 0)
  50.     {
  51.       points(i,j, pch=0, cex=sc*sqrt(match.preds))
  52.     }
  53.   }
  54. }
复制代码


7
Lisrelchen 发表于 2014-11-30 00:43:38
  1. # clears workspace:  
  2. rm(list=ls())

  3. # sets working directories:
  4. setwd("C:/Users/EJ/Dropbox/EJ/temp/BayesBook/test/ParameterEstimation/Binomial")

  5. library(R2WinBUGS)
  6. bugsdir <- "C:/Program Files/WinBUGS14"

  7. nmax <- 500
  8. k    <- c(16,18,22,25,27)
  9. m    <- length(k)

  10. data <- list("nmax", "k", "m") # to be passed on to WinBUGS
  11. myinits <-        list(
  12.   list(theta = 0.5, n = nmax/2))

  13. # parameters to be monitored:       
  14. parameters <- c("theta", "n")

  15. # The following command calls WinBUGS with specific options.
  16. # For a detailed description see Sturtz, Ligges, & Gelman (2005).
  17. samples <- bugs(data, inits=myinits, parameters,
  18.                                  model.file ="Survey.txt",
  19.                                  n.chains=1, n.iter=5000, n.burnin=1, n.thin=1,
  20.                                  DIC=T, bugs.directory=bugsdir,
  21.                                  codaPkg=F, debug=T)
  22. # Now the values for the monitored parameters are in the "samples" object,
  23. # ready for inspection.

  24. theta      <- samples$sims.list$theta
  25. n          <- samples$sims.list$n

  26. ## First calculate MLE:
  27. cc <- -Inf
  28. ind <- 0

  29. for (i in 1:length(n))
  30. {
  31.         logL <- 0
  32.         for(j in 1:m)
  33.         {               
  34.                 logL <- logL+lgamma(n[i]+1)-lgamma(k[j]+1)-lgamma(n[i]-k[j]+1)
  35.                 logL <- logL+k[j]*log(theta[i])+(n[i]-k[j])*log(1-theta[i])
  36.         }
  37.         if (logL>cc)
  38.   {
  39.                 ind <- i
  40.                 cc <- logL
  41.         }
  42. }
  43. # end MLE

  44. ######################Plots########################################################################
  45. layout(matrix(c(2,0,1,3),2,2,byrow=T), width=c(2/3, 1/3), heights=c(1/3,2/3))
  46. xhist <- hist(n,plot=F)
  47. yhist <- hist(theta,plot=F)
  48. top <- max(c(xhist$counts, yhist$counts))
  49. xrange <- c(0,nmax)
  50. yrange <- c(0,1)

  51. par(mar=c(5,5,1,1))
  52. plot(n,theta,xlim=xrange, ylim=yrange,ylab="", xlab="")
  53. axis(1)
  54. mtext("Number of Surveys", side=1,line=2.25, cex=1.2)
  55. axis(2, cex=1.2)
  56. las=0
  57. mtext("Rate of Return", side=2 ,line=2.25, cex=1.2)
  58. las=1
  59. points(mean(n),mean(theta), col="red", lwd=3, pch=4) #expectation
  60. points(n[ind],theta[ind], col="green", lwd=3, pch=10) #Maximum Likelihood

  61. par(mar=c(0,4,1,1))
  62. barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0,col="lightblue")

  63. par(mar=c(4,0,1,3))
  64. barplot(yhist$counts, axes=FALSE, xlim=c(0, top), space=0, horiz=TRUE,col="lightblue")
复制代码


8
Lisrelchen 发表于 2014-11-30 00:45:33
  1. # clears workspace:  
  2. rm(list=ls())

  3. # sets working directories:
  4. setwd("C:/Users/EJ/Dropbox/EJ/temp/BayesBook/test/ParameterEstimation/DataAnalysis")
  5. library(R2WinBUGS)
  6. bugsdir <- "C:/Program Files/WinBUGS14"

  7. c <- scan("changepointdata.txt")
  8. n <- length(c)
  9. t <- 1:n

  10. data <- list("c", "n", "t") # to be passed on to WinBUGS
  11. myinits <- list(
  12.   list(mu = c(1,1), lambda = 1, tau = n/2))

  13. # parameters to be monitored:       
  14. parameters <- c("mu","sigma","tau")

  15. # The following command calls WinBUGS with specific options.
  16. # For a detailed description see Sturtz, Ligges, & Gelman (2005).
  17. samples <- bugs(data, inits=myinits, parameters,
  18.                                  model.file ="ChangeDetection.txt",
  19.                                  n.chains=1, n.iter=1000, n.burnin=1, n.thin=1,
  20.                                  DIC=T, bugs.directory=bugsdir,
  21.                                  codaPkg=F, debug=T)
  22. # Now the values for the monitored parameters are in the "samples" object,
  23. # ready for inspection.

  24. plot(samples)
  25. mean.tau <- samples$mean$tau
  26. mean.mu1 <- samples$mean$mu[1]
  27. mean.mu2 <- samples$mean$mu[2]

  28. #some plotting options to make things look better:
  29. par(cex.main = 1.5, mar = c(5, 6, 4, 5) + 0.1, mgp = c(3.5, 1, 0), cex.lab = 1.5,
  30.     font.lab = 2, cex.axis = 1.3, bty = "n", las=1)
  31. # the plotting:
  32. plot(c, type="l", main="", ylab="Values", xlab="Samples")
  33. lines(c(1, mean.tau), c(mean.mu1,mean.mu1), lwd=2, col="red")
  34. lines(c(mean.tau+1,length(c)), c(mean.mu2,mean.mu2), lwd=2, col="red")
复制代码


9
Lisrelchen 发表于 2014-11-30 00:47:07
  1. #### After 949 failed attempts, Cha Sa-Soon from Korea finally passed   
  2. #### her theoretical drivers exam. What can we infer about theta, the
  3. #### probability of answering any one question correctly?
  4.      
  5. # clears workspace:  
  6. rm(list=ls())

  7. # sets working directories:
  8. setwd("C:/Users/EJ/Dropbox/EJ/temp/BayesBook/test/ParameterEstimation/DataAnalysis")
  9. library(R2WinBUGS)
  10. bugsdir = "C:/Program Files/WinBUGS14"

  11. nattempts  <- 950   
  12. nfails     <- 949   
  13. n          <- 50    # Number of questions
  14. z = c(rep(NA,nfails),30)
  15. y = c(rep(1,nfails),0)

  16. data <- list("nattempts","n","z","y") # to be passed on to WinBUGS

  17. myinits <- list(
  18.   list(theta = 0.5))

  19. parameters <- c("theta")

  20. # The following command calls WinBUGS with specific options.
  21. # For a detailed description see Sturtz, Ligges, & Gelman (2005).
  22. samples <- bugs(data, inits=myinits, parameters,
  23.                                  model.file ="ChaSaSoon.txt",
  24.                                  n.chains=1, n.iter=4100, n.burnin=100, n.thin=1,
  25.                                  DIC=T, bugs.directory=bugsdir, #DIC=T or else error message
  26.                                  codaPkg=F, debug=T)
  27. # Now the values for theta are in the "samples" object, ready for inspection.

  28. # Collect all samples in "theta":
  29. theta <- samples$sims.list$theta

  30. # Plot the posterior for theta:
  31. par(cex.main = 1.5, mar = c(5, 6, 4, 5) + 0.1, mgp = c(3.5, 1, 0), cex.lab = 1.5,
  32.     font.lab = 2, cex.axis = 1.3, bty = "n", las=1)
  33. plot(density(theta), ylim=c(0,200), xlim=c(0.2,0.5), lty=1, lwd=2,
  34.      axes=F, main=" ", xlab=" ", ylab="Posterior Density")
  35. axis(1, at = c(0.20, 0.30, 0.40, 0.50), lab=c("0.20", "0.30", "0.40", "0.50"))
  36. axis(2)
  37. mtext(expression(paste(theta," for Cha Sa-soon")), side=1, line = 2.8, cex=2)

  38. # plot 95% confidence interval
  39. x0 <- quantile(theta,p=c(.025,.975))[[1]]
  40. x1 <- quantile(theta,p=c(.025,.975))[[2]]
  41. arrows(x0, 150, x1, 150, length = 0.05, angle = 90, code = 3, lwd=2)
  42. text((x0+x1)/2, 170, labels="95%", cex = 1.5)
  43. text(x0-.03, 150, labels=as.character(round(x0,2)), cex = 1.5)
  44. text(x0+.04, 150, labels=as.character(round(x1,2)), cex = 1.5)
复制代码


10
bailihongchen 发表于 2014-12-13 14:28:25
感谢楼主的分享,顶一个

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

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2025-12-22 08:27