Skip to content

Commit 3a6887d

Browse files
committed
separate models and data 1
1 parent 7017e58 commit 3a6887d

21 files changed

+227
-240
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
k <- 5
2+
n <- 10

Bayesian_Cognitive_Modeling/GettingStarted/Rate_1_Stan.R

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,12 @@
33
# what it does.
44

55
# clears workspace:
6-
rm(list=ls(all=TRUE))
6+
rm(list=ls())
77

88
library(rstan)
99

10-
model <- "
11-
// Inferring a Rate
12-
data {
13-
int<lower=1> n;
14-
int<lower=0> k;
15-
}
16-
parameters {
17-
real<lower=0,upper=1> theta;
18-
}
19-
model {
20-
// Prior Distribution for Rate Theta
21-
theta ~ beta(1, 1);
22-
23-
// Observed Counts
24-
k ~ binomial(n, theta);
25-
}"
26-
27-
k <- 5
28-
n <- 10
29-
30-
data <- list(k=k, n=n) # to be passed on to Stan
10+
# to be passed on to Stan
11+
data <- read_rdump("Rate_1.data.R")
3112

3213
myinits <- list(
3314
list(theta=.1), # chain 1 starting value
@@ -38,16 +19,16 @@ parameters <- c("theta")
3819

3920
# The following command calls Stan with specific options.
4021
# For a detailed description type "?rstan".
41-
samples <- stan(model_code=model,
22+
samples <- stan(file="Rate_1_model.stan",
4223
data=data,
4324
init=myinits, # If not specified, gives random inits
4425
pars=parameters,
45-
iter=40000,
26+
iter=20000,
4627
chains=2,
4728
thin=1,
4829
# warmup = 100, # Stands for burn-in; Default = iter/2
4930
# seed = 123 # Setting seed; Default is random seed
50-
)
31+
)
5132
# Now the values for the monitored parameters are in the "samples" object,
5233
# ready for inspection.
5334

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Inferring a Rate
2+
data {
3+
int<lower=1> n;
4+
int<lower=0> k;
5+
}
6+
parameters {
7+
real<lower=0,upper=1> theta;
8+
}
9+
model {
10+
// Prior Distribution for Rate Theta
11+
theta ~ beta(1, 1);
12+
13+
// Observed Counts
14+
k ~ binomial(n, theta);
15+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
k <- 5
2+
n <- 10

Bayesian_Cognitive_Modeling/ParameterEstimation/Binomial/Rate_1_Stan.R

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,46 +7,28 @@ rm(list=ls())
77

88
library(rstan)
99

10-
model <- "
11-
// Inferring a Rate
12-
data {
13-
int<lower=1> n;
14-
int<lower=0> k;
15-
}
16-
parameters {
17-
real<lower=0,upper=1> theta;
18-
}
19-
model {
20-
// Prior Distribution for Rate Theta
21-
theta ~ beta(1, 1);
22-
// Observed Counts
23-
k ~ binomial(n, theta);
24-
}"
25-
26-
k <- 5
27-
n <- 10
28-
29-
data <- list(k=k, n=n) # to be passed on to Stan
10+
# to be passed on to Stan
11+
data <- read_rdump("Rate_1.data.R")
3012

3113
myinits <- list(
32-
list(theta=0.1), #chain 1 starting value
33-
list(theta=0.9)) #chain 2 starting value
14+
list(theta=.1), # chain 1 starting value
15+
list(theta=.9)) # chain 2 starting value
3416

3517
# parameters to be monitored:
3618
parameters <- c("theta")
3719

3820
# The following command calls Stan with specific options.
39-
# For a detailed description type "?rstan".
40-
samples <- stan(model_code=model,
21+
# For a detailed description type "?stan".
22+
samples <- stan(file="Rate_1_model.stan",
4123
data=data,
4224
init=myinits, # If not specified, gives random inits
4325
pars=parameters,
44-
iter=40000,
26+
iter=20000,
4527
chains=2,
4628
thin=1,
47-
# warmup = 100, # Stands for burn-in; Default = iter/2
48-
# seed = 123 # Setting seed; Default is random seed
49-
)
29+
# warmup=100, # Stands for burn-in; Default = iter/2
30+
# seed=123 # Setting seed; Default is random seed
31+
)
5032
# Now the values for the monitored parameters are in the "samples" object,
5133
# ready for inspection.
5234

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Inferring a Rate
2+
data {
3+
int<lower=1> n;
4+
int<lower=0> k;
5+
}
6+
parameters {
7+
real<lower=0,upper=1> theta;
8+
}
9+
model {
10+
// Prior Distribution for Rate Theta
11+
theta ~ beta(1, 1);
12+
13+
// Observed Counts
14+
k ~ binomial(n, theta);
15+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
k1 <- 5
2+
k2 <- 7
3+
n1 <- 10
4+
n2 <- 10

Bayesian_Cognitive_Modeling/ParameterEstimation/Binomial/Rate_2_Stan.R

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,37 +3,7 @@ rm(list=ls())
33

44
library(rstan)
55

6-
model <- "
7-
// Difference Between Two Rates
8-
data {
9-
int<lower=1> n1;
10-
int<lower=1> n2;
11-
int<lower=0> k1;
12-
int<lower=0> k2;
13-
}
14-
parameters {
15-
real<lower=0,upper=1> theta1;
16-
real<lower=0,upper=1> theta2;
17-
}
18-
transformed parameters {
19-
real<lower=-1,upper=1> delta;
20-
delta <- theta1 - theta2;
21-
}
22-
model {
23-
// Prior Distribution for Rate Theta
24-
theta1 ~ beta(1, 1);
25-
theta2 ~ beta(1, 1);
26-
// Observed Counts
27-
k1 ~ binomial(n1, theta1);
28-
k2 ~ binomial(n2, theta2);
29-
}"
30-
31-
k1 <- 5
32-
k2 <- 7
33-
n1 <- 10
34-
n2 <- 10
35-
36-
data <- list(k1=k1, k2=k2, n1=n1, n2=n2) # to be passed on to Stan
6+
data <- read_rdump("Rate_2,3.data.R") # to be passed on to Stan
377

388
myinits <- list(
399
list(theta1=0.1, theta2=0.9))
@@ -42,8 +12,8 @@ myinits <- list(
4212
parameters <- c("delta", "theta1", "theta2")
4313

4414
# The following command calls Stan with specific options.
45-
# For a detailed description type "?rstan".
46-
samples <- stan(model_code=model,
15+
# For a detailed description type "?stan".
16+
samples <- stan(file="Rate_2_model.stan",
4717
data=data,
4818
init=myinits, # If not specified, gives random inits
4919
pars=parameters,
@@ -52,7 +22,7 @@ samples <- stan(model_code=model,
5222
thin=1,
5323
# warmup=100, # Stands for burn-in; Default = iter/2
5424
# seed=123 # Setting seed; Default is random seed
55-
)
25+
)
5626
# Now the values for the monitored parameters are in the "samples" object,
5727
# ready for inspection.
5828

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Difference Between Two Rates
2+
data {
3+
int<lower=1> n1;
4+
int<lower=1> n2;
5+
int<lower=0> k1;
6+
int<lower=0> k2;
7+
}
8+
parameters {
9+
real<lower=0,upper=1> theta1;
10+
real<lower=0,upper=1> theta2;
11+
}
12+
transformed parameters {
13+
real<lower=-1,upper=1> delta;
14+
delta <- theta1 - theta2;
15+
}
16+
model {
17+
// Prior Distribution for Rate Theta
18+
theta1 ~ beta(1, 1);
19+
theta2 ~ beta(1, 1);
20+
// Observed Counts
21+
k1 ~ binomial(n1, theta1);
22+
k2 ~ binomial(n2, theta2);
23+
}

Bayesian_Cognitive_Modeling/ParameterEstimation/Binomial/Rate_3_Stan.R

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,7 @@ rm(list=ls())
33

44
library(rstan)
55

6-
model <- "
7-
// Inferring a Common Rate
8-
data {
9-
int<lower=1> n1;
10-
int<lower=1> n2;
11-
int<lower=0> k1;
12-
int<lower=0> k2;
13-
}
14-
parameters {
15-
real<lower=0,upper=1> theta;
16-
}
17-
model {
18-
// Prior on Single Rate Theta
19-
theta ~ beta(1, 1);
20-
// Observed Counts
21-
k1 ~ binomial(n1, theta);
22-
k2 ~ binomial(n2, theta);
23-
}"
24-
25-
k1 <- 5
26-
k2 <- 7
27-
n1 <- 10
28-
n2 <- 10
29-
30-
data <- list(k1=k1, k2=k2, n1=n1, n2=n2) # to be passed on to Stan
6+
data <- read_rdump("Rate_2,3.data.R") # to be passed on to Stan
317

328
myinits <- list(
339
list(theta=0.5))
@@ -36,8 +12,8 @@ myinits <- list(
3612
parameters <- c("theta")
3713

3814
# The following command calls Stan with specific options.
39-
# For a detailed description type "?rstan".
40-
samples <- stan(model_code=model,
15+
# For a detailed description type "?stan".
16+
samples <- stan(file="Rate_3_model.stan",
4117
data=data,
4218
init=myinits, # If not specified, gives random inits
4319
pars=parameters,
@@ -46,7 +22,7 @@ samples <- stan(model_code=model,
4622
thin=1,
4723
# warmup = 100, # Stands for burn-in; Default = iter/2
4824
# seed = 123 # Setting seed; Default is random seed
49-
)
25+
)
5026
print(samples)
5127

5228
theta <- extract(samples)$theta

0 commit comments

Comments
 (0)