Advertisement
Guest User

Untitled

a guest
Sep 12th, 2014
349
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. #install.packages(“msm”)#used for truncated normal dist when generating data
  2. #install.packages(“rstan”)
  3. require(rstan)
  4. require(msm)
  5.  
  6. ######
  7. generate.data<-function(c=1, k=1, s0=10, e0=100, t=1:500){
  8. z=c*e0^(-k/(c+k))*s0^(-c/(c+k))*t - (e0/s0)^(c/(c+k)) + (c/k)*(s0/e0)^(k/(c+k))
  9.  
  10. p=matrix(nrow=length(z))
  11. for(i in 1:length(z)){
  12. f<-function(x) (x/(1-x))^(k/(c+k))- ((1-x)/x)^(c/(c+k))-z[i]
  13. p[i]<-uniroot(f, interval= c(0,1), tol=10^-9)$root
  14. p[i]<-rtnorm(n=1,mean=p[i], sd=.05, lower=0, upper=1)
  15. }
  16. return(p)
  17. }
  18.  
  19. subj1<-generate.data(c=1, k=1, s0=10, e0=100, t=1:500)
  20. subj2<-generate.data(c=.5,k=.5, s0=10, e0=100, t=1:500)
  21. subj3<-generate.data(c=.3,k=.3, s0=10, e0=100, t=1:500)
  22. subj4<-generate.data(c=.25,k=.25, s0=10, e0=100, t=1:500)
  23.  
  24. dat<-rbind(subj1,subj2,subj3,subj4)
  25. dat<-cbind(rep(1:length(subj1),4),dat)
  26. dat<-cbind(rep(1:4,each=length(subj1)),dat)
  27. colnames(dat)<-c("Subj","Time","y")
  28.  
  29. ####
  30.  
  31.  
  32. Ndata=nrow(dat)
  33. Nsubj=length(unique(dat[,"Subj"]))
  34. subj=dat[,"Subj"]
  35. y=dat[,"y"]
  36. t=dat[,"Time"]
  37. dat2<- list(Ndata=Ndata, Nsubj=Nsubj, subj=subj, y=y, t = t)
  38.  
  39.  
  40. ####
  41. STAN_code <- '
  42. data {
  43. int<lower=0> Ndata;
  44. int<lower=0> Nsubj;
  45. int<lower=0> subj[Ndata];
  46. real y[Ndata];
  47. int<lower=0> t[Ndata];
  48. }
  49. parameters {
  50. real<lower=1, upper=1000> s0;
  51. real<lower=1, upper=1000> e0;
  52. real<lower=0, upper=1> k[Nsubj];
  53. }
  54. model {
  55. real z[Ndata];
  56. real p[Ndata];
  57.  
  58. for(i in 1:Ndata){
  59. z[i]<-(k[subj[i]]*t[i] +s0 -e0)/(s0*e0)^.5;
  60. p[i]<-.5*(z[i]/((z[i])^2+4)^.5 +1);
  61. }
  62. y ~ normal(p , .05 );
  63.  
  64. }
  65. '
  66.  
  67. ####
  68. fit <- stan(model_code = STAN_code, data = dat2, iter = 10000, chains = 3, warmup=500)
  69. fit2<-extract(fit)
  70. #plot(fit)
  71. #traceplot(fit,ask=T)
  72.  
  73.  
  74. ####Make Plots
  75. par(mar=c(4.1,4.1,2.1,2.1))
  76. par(mfrow=c(Nsubj+1,2))
  77. s0<-mean(fit2$s0)
  78. e0<-mean(fit2$e0)
  79. hist(fit2$s0, main="s0", xlab="")
  80. hist(fit2$e0, main="e0", xlab="")
  81. for(s in 1:Nsubj){
  82. k<-mean(fit2$k[,s])
  83. c=k;
  84. t=1:500
  85. z=c*e0^(-k/(c+k))*s0^(-c/(c+k))*t - (e0/s0)^(c/(c+k)) + (c/k)*(s0/e0)^(k/(c+k))
  86. p=matrix(nrow=length(z))
  87. for(i in 1:length(z)){
  88. f<-function(x) (x/(1-x))^(k/(c+k))- ((1-x)/x)^(c/(c+k))-z[i]
  89. p[i]<-uniroot(f, interval= c(0,1), tol=10^-9)$root
  90. }
  91.  
  92. hist(fit2$k[,s], main=paste("k",s), xlab="")
  93. plot(t,dat[which(dat[,"Subj"]==s),"y"], ylim=c(0,1),
  94. ylab="p", xlab="t", main=paste("Subj",s))
  95. lines(t,p, lwd=4, col="Red")
  96. }
  97. par(mar=c(5.1,4.1,4.1,2.1))
  98. ####
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement