diff --git a/.RData b/.RData index 52ea8e4..2d5a335 100644 Binary files a/.RData and b/.RData differ diff --git a/.Rhistory b/.Rhistory index 3d2137d..b67e4b6 100644 --- a/.Rhistory +++ b/.Rhistory @@ -1,512 +1,512 @@ -m_SHW <- quap( -alist( -W ~ dnorm(mu,sigma), -mu <- a[S]+b[S]*(H-Hbar), -a[S] ~ dnorm(60,10), -b[S] ~ dunif(0,1), -sigma ~ dunif(0,10) -), data=dat +}" +data_m4.5 <- list( +N = nrow(d), +height = d$height, +weight_s = d$weight_s, +weight_s2 = d$weight_s2 ) -data.frame(dat) |> -ggplot(aes(x=H, y=W, color=factor(S)))+ -geom_point()+ -geom_smooth(method = lm)+ -# facet_wrap(~S)+ -theme_bw() -precis(m_SHW,depth=2) -dat <- list( -W = d$weight, -H = d$height, -Hbar = mean(d$height), -S = d$male+1 ) -m_SHW <- quap( -alist( -W ~ dnorm(mu,sigma), -mu <- a[S]+b[S]*(H-Hbar), -a[S] ~ dnorm(60,10), -b[S] ~ dunif(0,1), -sigma ~ dunif(0,10) -), data=dat -) -dat <- list( -W = d$weight, -H = d$height, -Hbar = mean(d$height), -S = d$male+1 ) -m_SHW <- quap( -alist( -W ~ dnorm(mu,sigma), -mu <- a[S]+b[S]*(H-Hbar), -a[S] ~ dnorm(60,10), -b[S] ~ dunif(0,1), -sigma ~ dunif(0,10) -), data=dat -) -data.frame(dat) |> -ggplot(aes(x=H, y=W, color=factor(S)))+ -geom_point()+ -geom_smooth(method = lm)+ -# facet_wrap(~S)+ -theme_bw() -precis(m_SHW,depth=2) -xseq <- seq(from=130,to=190, len=50) -muF <- -link(m_adults2, data=list(S=rep(1,50),H=xseq,Hbar=mean(d$height))) -plot(S) -plot(density(S)) -density(S) -hist(S) -S <- rnorm(1000, mean=2, sd=5) -hist(S) -plot(density(S)) -n <- 1000 -nsurveys <- 200 -meanSurveys <- vector() -sdSurveys <- vector() -for (i in 1:nsurveys) { -S <- sample(1:N,n) # indexes of people surveyed -mu_hat <- mean(realPop$vote[S]) -meanSurveys <- c(meanSurveys, mu_hat) -sdSurveys <- c(sdSurveys, sqrt(mu_hat*(1-mu_hat))) -} -n <- 1000 -nsurveys <- 200 -meanSurveys <- vector() -sdSurveys <- vector() -for (i in 1:nsurveys) { -S <- rnorm(n, mean=2, sd=5) -mu_hat <- mean(S) -sd_hat <- sd(S) -meanSurveys <- c(meanSurveys, mu_hat) # append mu_hat -sdSurveys <- c(sdSurveys, sd_hat) # append sd_hat -} -# Calculating MOE -MOE <- qnorm(.975)*sdSurveys/sqrt(n) -dataSampl <- data.frame ( -p = meanSurveys, -s = sdSurveys, -MOE = MOE, -lower = meanSurveys-qnorm(.975)*sqrt(pSurveys*(1-pSurveys)/n), -upper = meanSurveys+qnorm(.975)*sqrt(pSurveys*(1-pSurveys)/n) -) -n <- 1000 -nsurveys <- 200 -meanSurveys <- vector() -sdSurveys <- vector() -for (i in 1:nsurveys) { -S <- rnorm(n, mean=2, sd=5) -mu_hat <- mean(S) -sd_hat <- sd(S) -meanSurveys <- c(meanSurveys, mu_hat) # append mu_hat -sdSurveys <- c(sdSurveys, sd_hat) # append sd_hat -} -# Calculating MOE -MOE <- qnorm(.975)*sdSurveys/sqrt(n) -dataSampl <- data.frame ( -p = meanSurveys, -s = sdSurveys, -MOE = MOE, -lower = meanSurveys-qnorm(.975)*sdSurveys/sqrt(n), -upper = meanSurveys+qnorm(.975)*sdSurveys/sqrt(n) -) -View(dataSampl) -library(rethinking) -library(tidyverse) -library(dplyr) -library(ggplot2) -dataSampl %>% -# slice_head(n = 100) %>% -slice(sample(1:nsurveys,100)) %>% -ggplot(aes(x = c(1:100))) + -# geom_line(size = 0.8) + -geom_errorbar(aes(ymin=lower, ymax=upper), width=.1) + -geom_point(aes(y=p)) + -geom_hline(yintercept=TrueP, linetype="dashed") + -xlab("Sample of Approval Estimate") + ylab("95% I.C.") + -theme_bw() -dataSampl %>% -# slice_head(n = 100) %>% -slice(sample(1:nsurveys,100)) %>% -ggplot(aes(x = c(1:100))) + -# geom_line(size = 0.8) + -geom_errorbar(aes(ymin=lower, ymax=upper), width=.1) + -geom_point(aes(y=p)) + -# geom_hline(yintercept=TrueP, linetype="dashed") + -xlab("Sample of Approval Estimate") + ylab("95% I.C.") + -theme_bw() -sample(1:nsurveys,100) -dataSampl %>% -# slice_head(n = 100) %>% -slice(sample(1:nsurveys,100)) -dataSampl %>% -# slice_head(n = 100) %>% -slice(sample(1:nsurveys,100)) -dataSampl %>% -# slice_head(n = 100) %>% -slice(sample(1:nsurveys,100)) -dataSampl %>% -slice(sample(1:nsurveys,100)) %>% # select 100 samples -ggplot(aes(x = c(1:100))) + -# geom_line(size = 0.8) + -geom_errorbar(aes(ymin=lower, ymax=upper), width=.1) + -geom_point(aes(y=mu_hat)) + -# geom_hline(yintercept=TrueP, linetype="dashed") + -xlab("Sample of Approval Estimate") + ylab("95% I.C.") + -theme_bw() -n <- 1000 -nsurveys <- 200 -meanSurveys <- vector() -sdSurveys <- vector() -for (i in 1:nsurveys) { -S <- rnorm(n, mean=2, sd=5) -mu_hat <- mean(S) -sd_hat <- sd(S) -meanSurveys <- c(meanSurveys, mu_hat) # append mu_hat -sdSurveys <- c(sdSurveys, sd_hat) # append sd_hat -} -# Calculating MOE -MOE <- qnorm(.975)*sdSurveys/sqrt(n) -dataSampl <- data.frame ( -mu_hat = meanSurveys, -sd_hat = sdSurveys, -MOE = MOE, -lower = meanSurveys-qnorm(.975)*sdSurveys/sqrt(n), -upper = meanSurveys+qnorm(.975)*sdSurveys/sqrt(n) -) -dataSampl %>% -slice(sample(1:nsurveys,100)) %>% # select 100 samples -ggplot(aes(x = c(1:100))) + -# geom_line(size = 0.8) + -geom_errorbar(aes(ymin=lower, ymax=upper), width=.1) + -geom_point(aes(y=mu_hat)) + -# geom_hline(yintercept=TrueP, linetype="dashed") + -xlab("Sample of Approval Estimate") + ylab("95% I.C.") + -theme_bw() -n <- 1000 -nsurveys <- 200 -meanSurveys <- vector() -sdSurveys <- vector() -SSampl <- vector() -for (i in 1:nsurveys) { -S <- rnorm(n, mean=2, sd=5) -mu_hat <- mean(S) -sd_hat <- sd(S) -SSampl <- c(SSampl, S) -meanSurveys <- c(meanSurveys, mu_hat) # append mu_hat -sdSurveys <- c(sdSurveys, sd_hat) # append sd_hat -} -# Calculating MOE -MOE <- qnorm(.975)*sdSurveys/sqrt(n) -dataSampl <- data.frame ( -mu_hat = meanSurveys, -sd_hat = sdSurveys, -SSampl = SSampl, -MOE = MOE, -lower = meanSurveys-qnorm(.975)*sdSurveys/sqrt(n), -upper = meanSurveys+qnorm(.975)*sdSurveys/sqrt(n) +#fit_m4.5 <- stan(data = data_m4.5, model_code = stan_m4.5, +# chains = 3, iter = 1000) +fit_m4.6 <- stan(data = data_m4.5, model_code = stan_m4.6, +chains = 3, iter = 1000) +traceplot(fit_m4.5, pars = "b1") +traceplot(fit_m4.6, pars = "beta1") +traceplot(fit_m4.6, pars = "sigma") +print(fit_m4.6) +# Same Model in STAN +stan_m4.6 <- " +data { +int N; +vector[N] height; +vector[N] weight_s; +vector[N] weight_s2; +} +transformed data{ +real bar_x1; +real x1_sd; +vector[N] x1_std; +real y_sd; +bar_x1 = mean(weight_s); +x1_sd = sd(weight_s); +x1_std = (weight_s - bar_x1)/x1_sd; // centered and scaled +y_sd = sd(height); +} +parameters { +real alpha_std; +real beta1_std; +real beta3_std; +real sigma; +} +transformed parameters { +vector[N] mu; +for (i in 1:N) +mu[i] = alpha_std + beta1_std*weight_s[i] + +beta3_std*weight_s2[i]; +} +model { +// priors +alpha_std ~ normal(0,20); +beta1_std ~ normal(0,2); +beta3_std ~ normal(0,2); +sigma ~ uniform(0,50); +// sigma ~ exponential(1/y_sd); +// model +height ~normal(mu,sigma); +} +generated quantities { +real alpha; +real beta1; +real beta3; +alpha = alpha_std - (beta1_std*bar_x1)/x1_sd ++ (beta3_std*bar_x1^2)/x1_sd^2; +beta1 = beta1_std/x1_sd - 2*(beta3_std*bar_x1)/x1_sd^2; +beta3 = beta3_std/x1_sd^2; +}" +data_m4.6 <- list( +N = nrow(d), +height = d$height, +weight_s = d$weight_s, +weight_s2 = d$weight_s2 ) -View(dataSampl) -n <- 1000 -nsurveys <- 200 -meanSurveys <- vector() -sdSurveys <- vector() -SSampl <- vector() -for (i in 1:nsurveys) { -S <- rnorm(n, mean=2, sd=5) -mu_hat <- mean(S) -sd_hat <- sd(S) -SSampl <- c(SSampl, list(S)) -meanSurveys <- c(meanSurveys, mu_hat) # append mu_hat -sdSurveys <- c(sdSurveys, sd_hat) # append sd_hat -} -# Calculating MOE -MOE <- qnorm(.975)*sdSurveys/sqrt(n) -dataSampl <- data.frame ( -mu_hat = meanSurveys, -sd_hat = sdSurveys, -SSampl = SSampl, -MOE = MOE, -lower = meanSurveys-qnorm(.975)*sdSurveys/sqrt(n), -upper = meanSurveys+qnorm(.975)*sdSurveys/sqrt(n) +fit_m4.6 <- stan(data = data_m4.6, +model_code = stan_m4.6, +iter = 2000) +# Same Model in STAN +stan_m4.6 <- " +data { +int N; +vector[N] height; +vector[N] weight_s; +vector[N] weight_s2; +} +transformed data{ +real bar_x1; +real x1_sd; +vector[N] x1_std; +real y_sd; +bar_x1 = mean(weight_s); +x1_sd = sd(weight_s); +x1_std = (weight_s - bar_x1)/x1_sd; // centered and scaled +y_sd = sd(height); +} +parameters { +real alpha_std; +real beta1_std; +real beta3_std; +real sigma; +} +transformed parameters { +vector[N] mu; +for (i in 1:N) +mu[i] = alpha_std + beta1_std*weight_s[i] + +beta3_std*weight_s2[i]; +} +model { +// priors +alpha_std ~ normal(0,20); +beta1_std ~ normal(0,2); +beta3_std ~ normal(0,2); +sigma ~ uniform(0,8); +// sigma ~ exponential(1/y_sd); +// model +height ~normal(mu,sigma); +} +generated quantities { +real alpha; +real beta1; +real beta3; +alpha = alpha_std - (beta1_std*bar_x1)/x1_sd ++ (beta3_std*bar_x1^2)/x1_sd^2; +beta1 = beta1_std/x1_sd - 2*(beta3_std*bar_x1)/x1_sd^2; +beta3 = beta3_std/x1_sd^2; +}" +data_m4.6 <- list( +N = nrow(d), +height = d$height, +weight_s = d$weight_s, +weight_s2 = d$weight_s2 ) -View(dataSampl) -View(SSampl) -n <- 1000 -nsurveys <- 200 -meanSurveys <- vector() -sdSurveys <- vector() -SSampl <- vector() -for (i in 1:nsurveys) { -S <- rnorm(n, mean=2, sd=5) -mu_hat <- mean(S) -sd_hat <- sd(S) -SSampl <- c(SSampl, list(S)) -meanSurveys <- c(meanSurveys, mu_hat) # append mu_hat -sdSurveys <- c(sdSurveys, sd_hat) # append sd_hat -} -# Calculating MOE -MOE <- qnorm(.975)*sdSurveys/sqrt(n) -dataSampl <- data.frame ( -mu_hat = meanSurveys, -sd_hat = sdSurveys, -MOE = MOE, -lower = meanSurveys-qnorm(.975)*sdSurveys/sqrt(n), -upper = meanSurveys+qnorm(.975)*sdSurveys/sqrt(n) +fit_m4.6 <- stan(data = data_m4.6, +model_code = stan_m4.6, +iter = 2000) +# Same Model in STAN +stan_m4.6 <- " +data { +int N; +vector[N] height; +vector[N] weight_s; +vector[N] weight_s2; +} +transformed data{ +real bar_x1; +real x1_sd; +vector[N] x1_std; +real y_sd; +bar_x1 = mean(weight_s); +x1_sd = sd(weight_s); +x1_std = (weight_s - bar_x1)/x1_sd; // centered and scaled +y_sd = sd(height); +} +parameters { +real alpha_std; +real beta1_std; +real beta3_std; +real sigma; +} +transformed parameters { +vector[N] mu; +for (i in 1:N) +mu[i] = alpha_std + beta1_std*weight_s[i] + +beta3_std*weight_s2[i]; +} +model { +// priors +alpha_std ~ normal(0,20); +beta1_std ~ normal(0,2); +beta3_std ~ normal(0,2); +// sigma ~ uniform(0,50); +sigma ~ exponential(1/y_sd); +// model +height ~normal(mu,sigma); +} +generated quantities { +real alpha; +real beta1; +real beta3; +alpha = alpha_std - (beta1_std*bar_x1)/x1_sd ++ (beta3_std*bar_x1^2)/x1_sd^2; +beta1 = beta1_std/x1_sd - 2*(beta3_std*bar_x1)/x1_sd^2; +beta3 = beta3_std/x1_sd^2; +}" +data_m4.6 <- list( +N = nrow(d), +height = d$height, +weight_s = d$weight_s, +weight_s2 = d$weight_s2 ) -View(dataSampl) -dataSampl %>% -slice(sample(1:nsurveys,100)) %>% # select 100 samples -ggplot(aes(x = c(1:100))) + -# geom_line(size = 0.8) + -geom_errorbar(aes(ymin=lower, ymax=upper), width=.1) + -geom_point(aes(y=mu_hat)) + -# geom_hline(yintercept=TrueP, linetype="dashed") + -xlab("Sample of Approval Estimate") + ylab("95% I.C.") + -theme_bw() -SSlice <- sample(1:nsurveys,100) -dataSampl %>% -slice(SSlice) %>% # select 100 samples -ggplot(aes(x = c(1:100))) + -# geom_line(size = 0.8) + -geom_errorbar(aes(ymin=lower, ymax=upper), width=.1) + -geom_point(aes(y=mu_hat)) + -# geom_hline(yintercept=TrueP, linetype="dashed") + -xlab("Sample of Approval Estimate") + ylab("95% I.C.") + -theme_bw() -SSampl[SSlice] -n <- 100 -nsurveys <- 200 -meanSurveys <- vector() -sdSurveys <- vector() -SSampl <- vector() -for (i in 1:nsurveys) { -S <- rnorm(n, mean=2, sd=5) -mu_hat <- mean(S) -sd_hat <- sd(S) -SSampl <- c(SSampl, list(S)) -meanSurveys <- c(meanSurveys, mu_hat) # append mu_hat -sdSurveys <- c(sdSurveys, sd_hat) # append sd_hat -} -# Calculating MOE -MOE <- qnorm(.975)*sdSurveys/sqrt(n) -dataSampl <- data.frame ( -mu_hat = meanSurveys, -sd_hat = sdSurveys, -MOE = MOE, -lower = meanSurveys-qnorm(.975)*sdSurveys/sqrt(n), -upper = meanSurveys+qnorm(.975)*sdSurveys/sqrt(n) +fit_m4.6 <- stan(data = data_m4.6, +model_code = stan_m4.6, +iter = 2000) +traceplot(fit_m4.6, pars = "sigma") +# Same Model in STAN +stan_m4.6 <- " +data { +int N; +vector[N] height; +vector[N] weight_s; +vector[N] weight_s2; +} +transformed data{ +real bar_x1; +real x1_sd; +vector[N] x1_std; +real y_sd; +bar_x1 = mean(weight_s); +x1_sd = sd(weight_s); +x1_std = (weight_s - bar_x1)/x1_sd; // centered and scaled +y_sd = sd(height); +} +parameters { +real alpha_std; +real beta1_std; +real beta3_std; +real sigma; +} +transformed parameters { +vector[N] mu; +for (i in 1:N) +mu[i] = alpha_std + beta1_std*weight_s[i] + +beta3_std*weight_s2[i]; +} +model { +// priors +alpha_std ~ normal(0,20); +beta1_std ~ normal(0,2); +beta3_std ~ normal(0,2); +// sigma ~ uniform(0,50); +sigma ~ exponential(1/y_sd); +// model +height ~normal(mu,sigma); +} +generated quantities { +real alpha; +real beta1; +real beta3; +alpha = alpha_std - (beta1_std*bar_x1)/x1_sd ++ (beta3_std*bar_x1^2)/x1_sd^2; +beta1 = beta1_std/x1_sd - 2*(beta3_std*bar_x1)/x1_sd^2; +beta3 = beta3_std/x1_sd^2; +}" +data_m4.6 <- list( +N = nrow(d), +height = d$height, +weight_s = d$weight_s, +weight_s2 = d$weight_s2 ) -SSlice <- sample(1:nsurveys,100) -dataSampl %>% -slice(SSlice) %>% # select 100 samples -ggplot(aes(x = c(1:100))) + -# geom_line(size = 0.8) + -geom_errorbar(aes(ymin=lower, ymax=upper), width=.1) + -geom_point(aes(y=mu_hat)) + -# geom_hline(yintercept=TrueP, linetype="dashed") + -xlab("Sample of Approval Estimate") + ylab("95% I.C.") + -theme_bw() -SSampl[SSlice] -SSampl[[SSlice]] -SSampl[[82]] -SSampl[SSlice[1]] -SSampl[SSlice] -quantile(S,probs=c(.025,.975)) -n <- 100 -nsurveys <- 200 -meanSurveys <- vector() -sdSurveys <- vector() -SSampl <- vector() -percs <- vector() -for (i in 1:nsurveys) { -S <- rnorm(n, mean=2, sd=5) -mu_hat <- mean(S) -sd_hat <- sd(S) -SSampl <- c(SSampl, list(S)) -percs <- c(percs,quantile(S,probs=c(.025,.975))) -meanSurveys <- c(meanSurveys, mu_hat) # append mu_hat -sdSurveys <- c(sdSurveys, sd_hat) # append sd_hat -} -# Calculating MOE -MOE <- qnorm(.975)*sdSurveys/sqrt(n) -dataSampl <- data.frame ( -mu_hat = meanSurveys, -sd_hat = sdSurveys, -MOE = MOE, -lower = meanSurveys-qnorm(.975)*sdSurveys/sqrt(n), -upper = meanSurveys+qnorm(.975)*sdSurveys/sqrt(n) +fit_m4.6 <- stan(data = data_m4.6, +model_code = stan_m4.6, +chains = 4, +iter = 2000) +traceplot(fit_m4.6, pars = "sigma") +summary(fit_m4.6) +print(fit_m4.6, pars = c("alpha")) +print(fit_m4.6, pars = c("alpha")) +print(fit_m4.6, pars = c("alpha","beta1", "beta3")) +print(fit_m4.6, pars = c("alpha","beta1", "beta3", "sigma")) +print(fit_m4.6, pars = c("alpha","beta1", "beta3", "sigma")) +precis(m4.5) +print(fit_m4.6, pars = c("alpha","beta1", "beta3", "sigma")) +print(fit_m4.6, pars = c("alpha_std","beta1_std", "beta3_std")) +precis(m4.5) +print(fit_m4.6, pars = c("alpha","beta1", "beta3", "sigma")) +print(fit_m4.6, pars = c("alpha_std","beta1_std", "beta3_std", "sigma")) +precis(m4.5) +sigma_sample <- extract(fit_mn)[["sigma"]] +sigma_sample <- extract(fit_m4.6)[["sigma"]] +hist(sigma_sample) +# Same Model in STAN +stan_m4.6 <- " +data { +int N; +vector[N] height; +vector[N] weight_s; +vector[N] weight_s2; +} +transformed data{ +real bar_x1; +real x1_sd; +vector[N] x1_std; +real y_sd; +bar_x1 = mean(weight_s); +x1_sd = sd(weight_s); +x1_std = (weight_s - bar_x1)/x1_sd; // centered and scaled +y_sd = sd(height); +} +parameters { +real alpha_std; +real beta1_std; +real beta3_std; +real sigma; +} +transformed parameters { +vector[N] mu; +for (i in 1:N) +mu[i] = alpha_std + beta1_std*weight_s[i] + +beta3_std*weight_s2[i]; +} +model { +// priors +alpha_std ~ normal(0,20); +beta1_std ~ normal(0,2); +beta3_std ~ normal(0,2); +sigma ~ uniform(5,3); +// sigma ~ exponential(1/y_sd); // WHY????? +// model +height ~normal(mu,sigma); +} +generated quantities { +real alpha; +real beta1; +real beta3; +alpha = alpha_std - (beta1_std*bar_x1)/x1_sd ++ (beta3_std*bar_x1^2)/x1_sd^2; +beta1 = beta1_std/x1_sd - 2*(beta3_std*bar_x1)/x1_sd^2; +beta3 = beta3_std/x1_sd^2; +}" +data_m4.6 <- list( +N = nrow(d), +height = d$height, +weight_s = d$weight_s, +weight_s2 = d$weight_s2 ) -n <- 100 -nsurveys <- 200 -meanSurveys <- vector() -sdSurveys <- vector() -SSampl <- vector() -percs <- vector() -for (i in 1:nsurveys) { -S <- rnorm(n, mean=2, sd=5) -mu_hat <- mean(S) -sd_hat <- sd(S) -SSampl <- c(SSampl, list(S)) -percs <- c(percs,list(quantile(S,probs=c(.025,.975)))) -meanSurveys <- c(meanSurveys, mu_hat) # append mu_hat -sdSurveys <- c(sdSurveys, sd_hat) # append sd_hat -} -# Calculating MOE -MOE <- qnorm(.975)*sdSurveys/sqrt(n) -dataSampl <- data.frame ( -mu_hat = meanSurveys, -sd_hat = sdSurveys, -MOE = MOE, -lower = meanSurveys-qnorm(.975)*sdSurveys/sqrt(n), -upper = meanSurveys+qnorm(.975)*sdSurveys/sqrt(n) +fit_m4.6 <- stan(data = data_m4.6, +model_code = stan_m4.6, +chains = 4, +iter = 2000) +# Same Model in STAN +stan_m4.6 <- " +data { +int N; +vector[N] height; +vector[N] weight_s; +vector[N] weight_s2; +} +transformed data{ +real bar_x1; +real x1_sd; +vector[N] x1_std; +real y_sd; +bar_x1 = mean(weight_s); +x1_sd = sd(weight_s); +x1_std = (weight_s - bar_x1)/x1_sd; // centered and scaled +y_sd = sd(height); +} +parameters { +real alpha_std; +real beta1_std; +real beta3_std; +real sigma; +} +transformed parameters { +vector[N] mu; +for (i in 1:N) +mu[i] = alpha_std + beta1_std*weight_s[i] + +beta3_std*weight_s2[i]; +} +model { +// priors +alpha_std ~ normal(0,20); +beta1_std ~ normal(0,2); +beta3_std ~ normal(0,2); +sigma ~ uniform(5,3); +// sigma ~ exponential(1/y_sd); +// model +height ~normal(mu,sigma); +} +generated quantities { +real alpha; +real beta1; +real beta3; +alpha = alpha_std - (beta1_std*bar_x1)/x1_sd ++ (beta3_std*bar_x1^2)/x1_sd^2; +beta1 = beta1_std/x1_sd - 2*(beta3_std*bar_x1)/x1_sd^2; +beta3 = beta3_std/x1_sd^2; +}" +data_m4.6 <- list( +N = nrow(d), +height = d$height, +weight_s = d$weight_s, +weight_s2 = d$weight_s2 ) -View(percs) -percs[SSlice] -n <- 100 -nsurveys <- 200 -meanSurveys <- vector() -sdSurveys <- vector() -SSampl <- vector() -percs <- vector() -for (i in 1:nsurveys) { -S <- rnorm(n, mean=2, sd=5) -mu_hat <- mean(S) -sd_hat <- sd(S) -SSampl <- c(SSampl, list(S)) -percs <- c(percs,list(quantile(S,probs=c(.025,.975)))) -meanSurveys <- c(meanSurveys, mu_hat) # append mu_hat -sdSurveys <- c(sdSurveys, sd_hat) # append sd_hat -} -# Calculating MOE -MOE <- qnorm(.975)*sdSurveys/sqrt(n) -dataSampl <- data.frame ( -mu_hat = meanSurveys, -sd_hat = sdSurveys, -perclst = percs, -MOE = MOE, -lower = meanSurveys-qnorm(.975)*sdSurveys/sqrt(n), -upper = meanSurveys+qnorm(.975)*sdSurveys/sqrt(n) +fit_m4.6 <- stan(data = data_m4.6, +model_code = stan_m4.6, +chains = 4, +iter = 2000) +# Same Model in STAN +stan_m4.6 <- " +data { +int N; +vector[N] height; +vector[N] weight_s; +vector[N] weight_s2; +} +transformed data{ +real bar_x1; +real x1_sd; +vector[N] x1_std; +real y_sd; +bar_x1 = mean(weight_s); +x1_sd = sd(weight_s); +x1_std = (weight_s - bar_x1)/x1_sd; // centered and scaled +y_sd = sd(height); +} +parameters { +real alpha_std; +real beta1_std; +real beta3_std; +real sigma; +} +transformed parameters { +vector[N] mu; +for (i in 1:N) +mu[i] = alpha_std + beta1_std*weight_s[i] + +beta3_std*weight_s2[i]; +} +model { +// priors +alpha_std ~ normal(0,20); +beta1_std ~ normal(0,2); +beta3_std ~ normal(0,2); +sigma ~ normal(5,3); +// sigma ~ exponential(1/y_sd); +// model +height ~normal(mu,sigma); +} +generated quantities { +real alpha; +real beta1; +real beta3; +alpha = alpha_std - (beta1_std*bar_x1)/x1_sd ++ (beta3_std*bar_x1^2)/x1_sd^2; +beta1 = beta1_std/x1_sd - 2*(beta3_std*bar_x1)/x1_sd^2; +beta3 = beta3_std/x1_sd^2; +}" +data_m4.6 <- list( +N = nrow(d), +height = d$height, +weight_s = d$weight_s, +weight_s2 = d$weight_s2 ) -View(dataSampl) -quantile(SSampl[SSlice[3]],probs=c(.025,.975)) -quantile(SSampl[SSlice[3]],probs=c(.025,.975))) -SSampl[SSlice[3]] -quantile(SSampl[SSlice[3]],probs=c(.025,.975)) -SSampl[SSlice[3]] -quantile(as.array(SSampl[SSlice[3]]),probs=c(.025,.975)) -as.array(SSampl[SSlice[3]]) -L <- SSampl[SSlice[3]] -View(L) -L[1] -L[2 -L[2] -L[[1]] -quantile(L,probs=c(.025,.975)) -quantile(L[[1]],probs=c(.025,.975)) -L <- SSampl[SSlice[1]] -quantile(L[[1]],probs=c(.025,.975)) -percs[SSlice] -SSlice <- sample(1:nsurveys,100) -dataSampl %>% -slice(SSlice) %>% # select 100 samples -ggplot(aes(x = c(1:100))) + -geom_errorbar(aes(ymin=lower, ymax=upper), width=.1) + -geom_point(aes(y=mu_hat)) + -xlab("Sample of Approval Estimate") + ylab("95% I.C.") + -theme_bw() -percs[SSlice][1] -percs[SSlice][[1]] -percs[SSlice] -L <- SSampl[SSlice[1]] -quantile(L[[1]],probs=c(.025,.975)) -percs[SSlice] -percs[SSlice] -L <- SSampl[SSlice[1]] -quantile(L[[1]],probs=c(.025,.975)) -SSlice <- sample(1:nsurveys,100) -dataSampl %>% -slice(SSlice) %>% # select 100 samples -ggplot(aes(x = c(1:100))) + -geom_errorbar(aes(ymin=lower, ymax=upper), width=.1) + -geom_point(aes(y=mu_hat)) + -xlab("Sample of Approval Estimate") + ylab("95% I.C.") + -theme_bw() -SSlice <- sample(1:nsurveys,100) -dataSampl %>% -slice(SSlice) %>% # select 100 samples -ggplot(aes(x = c(1:100))) + -geom_errorbar(aes(ymin=lower, ymax=upper), width=.1) + -geom_point(aes(y=mu_hat)) + -xlab("Sample of Approval Estimate") + ylab("95% I.C.") + -theme_bw() -SSlice <- sample(1:nsurveys,100) -dataSampl %>% -slice(SSlice) %>% # select 100 samples -ggplot(aes(x = c(1:100))) + -geom_errorbar(aes(ymin=lower, ymax=upper), width=.1) + -geom_point(aes(y=mu_hat)) + -xlab("Sample of Approval Estimate") + ylab("95% I.C.") + -theme_bw() -set.seed(42) -SSlice <- sample(1:nsurveys,100) -dataSampl %>% -slice(SSlice) %>% # select 100 samples -ggplot(aes(x = c(1:100))) + -geom_errorbar(aes(ymin=lower, ymax=upper), width=.1) + -geom_point(aes(y=mu_hat)) + -xlab("Sample of Approval Estimate") + ylab("95% I.C.") + -theme_bw() -L <- SSampl[SSlice[1]] -quantile(L[[1]],probs=c(.025,.975)) -percs[SSlice] -percs[SSlice][1] -L <- SSampl[SSlice[1]] -quantile(L[[1]],probs=c(.025,.975)) -percs[SSlice][1] -i <- 5 -L <- SSampl[SSlice[i]] -quantile(L[[1]],probs=c(.025,.975)) -percs[SSlice][i] -i <- 5 -L <- SSampl[SSlice[i]] -# quantile(L[[1]],probs=c(.025,.975)) -percs[SSlice][i-1] -percs[SSlice][i] -library(rethinking) -library(tidyverse) -library(dplyr) -library(ggplot2) -S <- rbern(100)+1 -dat <- sim_HW(S, b=c(.5,.5), a=c(0,10)) -# Indirect effect (b) is the same for men and women -dat |> -ggplot(aes(x=H, y=W, color=factor(S)))+ -geom_point()+ -geom_smooth(method = lm)+ -# facet_wrap(~S)+ -theme_bw() -dat <- list( -W = d$weight, -H = d$height, -Hbar = mean(d$height), -S = d$male+1 ) -m_SHW <- quap( -alist( -W ~ dnorm(mu,sigma), -mu <- a[S]+b[S]*(H-Hbar), -a[S] ~ dnorm(60,10), -b[S] ~ dunif(0,1), -sigma ~ dunif(0,10) -), data=dat +fit_m4.6 <- stan(data = data_m4.6, +model_code = stan_m4.6, +chains = 4, +iter = 2000) +print(fit_m4.6, pars = c("alpha","beta1", "beta3", "sigma")) +print(fit_m4.6, pars = c("alpha_std","beta1_std", "beta3_std", "sigma")) +precis(m4.5) +traceplot(fit_m4.6, pars = "sigma") +# Same Model in STAN +stan_m4.6 <- " +data { +int N; +vector[N] height; +vector[N] weight_s; +vector[N] weight_s2; +} +transformed data{ +real bar_x1; +real x1_sd; +vector[N] x1_std; +real y_sd; +bar_x1 = mean(weight_s); +x1_sd = sd(weight_s); +x1_std = (weight_s - bar_x1)/x1_sd; // centered and scaled +y_sd = sd(height); +} +parameters { +real alpha_std; +real beta1_std; +real beta3_std; +real sigma; +} +transformed parameters { +vector[N] mu; +for (i in 1:N) +mu[i] = alpha_std + beta1_std*weight_s[i] + +beta3_std*weight_s2[i]; +} +model { +// priors +alpha_std ~ normal(0,20); +beta1_std ~ normal(0,2); +beta3_std ~ normal(0,2); +sigma ~ normal(5,1.5); +// sigma ~ exponential(1/y_sd); +// model +height ~normal(mu,sigma); +} +generated quantities { +real alpha; +real beta1; +real beta3; +alpha = alpha_std - (beta1_std*bar_x1)/x1_sd ++ (beta3_std*bar_x1^2)/x1_sd^2; +beta1 = beta1_std/x1_sd - 2*(beta3_std*bar_x1)/x1_sd^2; +beta3 = beta3_std/x1_sd^2; +}" +data_m4.6 <- list( +N = nrow(d), +height = d$height, +weight_s = d$weight_s, +weight_s2 = d$weight_s2 ) -data.frame(dat) |> -ggplot(aes(x=H, y=W, color=factor(S)))+ -geom_point()+ -geom_smooth(method = lm)+ -# facet_wrap(~S)+ -theme_bw() -precis(m_SHW,depth=2) -post <- extract.samples(m_SHW) -dens(post$b[,1],xlim=c(.3, .9),ylim=c(0,8), -lwd=3,col=2,xlab="posterior mean weight(kg)") -dens(post$b[,2],lwd=3,col=4,add=TRUE) +fit_m4.6 <- stan(data = data_m4.6, +model_code = stan_m4.6, +chains = 4, +iter = 2000) +print(fit_m4.6, pars = c("alpha","beta1", "beta3", "sigma")) +print(fit_m4.6, pars = c("alpha_std","beta1_std", "beta3_std", "sigma")) +precis(m4.5) +traceplot(fit_m4.6, pars = "sigma") +sigma_sample <- extract(fit_m4.6)[["sigma"]] +hist(sigma_sample) diff --git a/.Rproj.user/B584AE7E/explorer-cache/4e0121cfc9e74692bb952169ff077b78 b/.Rproj.user/B584AE7E/explorer-cache/4e0121cfc9e74692bb952169ff077b78 new file mode 100644 index 0000000..75bf5c6 Binary files /dev/null and b/.Rproj.user/B584AE7E/explorer-cache/4e0121cfc9e74692bb952169ff077b78 differ diff --git a/.Rproj.user/B584AE7E/pcs/windowlayoutstate.pper b/.Rproj.user/B584AE7E/pcs/windowlayoutstate.pper index 20e838b..ba9f07e 100644 --- a/.Rproj.user/B584AE7E/pcs/windowlayoutstate.pper +++ b/.Rproj.user/B584AE7E/pcs/windowlayoutstate.pper @@ -1,14 +1,14 @@ { "left": { - "splitterpos": 129, + "splitterpos": 128, "topwindowstate": "NORMAL", - "panelheight": 803, - "windowheight": 841 + "panelheight": 959, + "windowheight": 997 }, "right": { - "splitterpos": 504, + "splitterpos": 601, "topwindowstate": "NORMAL", - "panelheight": 803, - "windowheight": 841 + "panelheight": 959, + "windowheight": 997 } } \ No newline at end of file diff --git a/.Rproj.user/B584AE7E/sources/per/t/18D958F7 b/.Rproj.user/B584AE7E/sources/per/t/18D958F7 new file mode 100644 index 0000000..0bf4166 --- /dev/null +++ b/.Rproj.user/B584AE7E/sources/per/t/18D958F7 @@ -0,0 +1,34 @@ +{ + "id": "18D958F7", + "path": null, + "project_path": null, + "type": "r_dataframe", + "hash": "0", + "contents": "", + "dirty": false, + "created": 1674955646734.0, + "source_on_save": false, + "relative_order": 3, + "properties": { + "expression": "d", + "caption": "d", + "totalObservations": "544", + "displayedObservations": "544", + "variables": "6", + "cacheKey": "1A4706DF", + "object": "d", + "environment": "", + "contentUrl": "grid_resource/gridviewer.html?env=&obj=d&cache_key=1A4706DF&max_cols=50", + "preview": "0", + "source_window_id": "", + "Source": "Source" + }, + "folds": "", + "lastKnownWriteTime": 7305808869231650162, + "encoding": "", + "collab_server": "", + "source_window": "", + "last_content_update": 1674955646734, + "read_only": false, + "read_only_alternatives": [] +} \ No newline at end of file diff --git a/.Rproj.user/B584AE7E/sources/per/t/18D958F7-contents b/.Rproj.user/B584AE7E/sources/per/t/18D958F7-contents new file mode 100644 index 0000000..e69de29 diff --git a/.Rproj.user/B584AE7E/sources/per/t/77F68D30 b/.Rproj.user/B584AE7E/sources/per/t/77F68D30 index 302786d..f4083a5 100644 --- a/.Rproj.user/B584AE7E/sources/per/t/77F68D30 +++ b/.Rproj.user/B584AE7E/sources/per/t/77F68D30 @@ -3,7 +3,7 @@ "path": "C:/Users/lacor/OneDrive/100. Personal/SelfDevelopment/25. Project 2022/Statistical-Thinking-2023/Statistical Thinking 2023_v2.qmd", "project_path": "Statistical Thinking 2023_v2.qmd", "type": "quarto_markdown", - "hash": "3741477333", + "hash": "59382345", "contents": "", "dirty": false, "created": 1673908998203.0, @@ -16,16 +16,17 @@ "Source": "Source", "docOutlineVisible": "1", "rmdVisualCollapsedChunks": "", - "rmdVisualModeLocation": "359:954", - "cursorPosition": "448,0", - "scrollLine": "0" + "rmdVisualModeLocation": "14113:13929", + "cursorPosition": "734,0", + "scrollLine": "0", + "docOutlineSize": "118" }, "folds": "", - "lastKnownWriteTime": 1674288067, + "lastKnownWriteTime": 1674978203, "encoding": "UTF-8", "collab_server": "", "source_window": "", - "last_content_update": 1674701597251, + "last_content_update": 1674978203724, "read_only": false, "read_only_alternatives": [] } \ No newline at end of file diff --git a/.Rproj.user/B584AE7E/sources/per/t/77F68D30-contents b/.Rproj.user/B584AE7E/sources/per/t/77F68D30-contents index ef06545..e4a5e5c 100644 --- a/.Rproj.user/B584AE7E/sources/per/t/77F68D30-contents +++ b/.Rproj.user/B584AE7E/sources/per/t/77F68D30-contents @@ -12,6 +12,7 @@ library(rethinking) library(tidyverse) library(dplyr) library(ggplot2) +library(rstan) ``` ## Lecture 1 @@ -459,9 +460,9 @@ percs[SSlice][i] ### Workflow ```{r} - data(Howell1) -d <-Howell1[Howell1$age>=18,] +# d <-Howell1[Howell1$age>=18,] +d <-Howell1 ``` ```{r} @@ -661,3 +662,164 @@ dens(post$b[,2],lwd=3,col=4,add=TRUE) ```{r} ``` + +### Polynomials + +```{r} +plot(height~weight,d) +``` + +```{r} +d$weight_s <-(d$weight-mean(d$weight))/sd(d$weight) +d$weight_s2 <-d$weight_s^2 +m4.5 <-quap( + alist( + height ~dnorm(mu,sigma), + mu <-a+b1*weight_s+b2*weight_s2, + a ~dnorm(178,20), + b1 ~dlnorm(0,1), + b2 ~dnorm(0,1), + sigma ~dunif(0,50) + ) ,data=d) +``` + +```{r} +precis(m4.5) +``` + +```{r} +# Not working - To be Reviewed +stan_m4.5 <- " +data { + int N; + vector[N] height; + vector[N] weight_s; + vector[N] weight_s2; +} +parameters { + real a; + real b1; + real b2; + real sigma; +} + +transformed parameters { + vector[N] mu; + + for (i in 1:N) + mu[i] = a + b1*weight_s[i] + b2*weight_s2[i]; +} + +model { + // priors + a ~ normal(178,20); + b1 ~ normal(0,1); + b2 ~ normal(0,1); + sigma ~ uniform(0,50); + + // model + height ~normal(mu,sigma); +}" +data_m4.5 <- list( + N = nrow(d), + height = d$height, + weight_s = d$weight_s, + weight_s2 = d$weight_s2 +) + +#fit_m4.5 <- stan(data = data_m4.5, model_code = stan_m4.5, +# chains = 3, iter = 1000) + +``` + +```{r} +# Same Model in STAN +stan_m4.6 <- " +data { + int N; + vector[N] height; + vector[N] weight_s; + vector[N] weight_s2; +} + +transformed data{ + real bar_x1; + real x1_sd; + vector[N] x1_std; + real y_sd; + + bar_x1 = mean(weight_s); + x1_sd = sd(weight_s); + x1_std = (weight_s - bar_x1)/x1_sd; // centered and scaled + + y_sd = sd(height); +} + +parameters { + real alpha_std; + real beta1_std; + real beta3_std; + real sigma; +} + +transformed parameters { + vector[N] mu; + + for (i in 1:N) + mu[i] = alpha_std + beta1_std*weight_s[i] + + beta3_std*weight_s2[i]; +} + +model { + // priors + alpha_std ~ normal(0,20); + beta1_std ~ normal(0,2); + beta3_std ~ normal(0,2); + sigma ~ normal(5,1.5); + // sigma ~ exponential(1/y_sd); + + // model + height ~normal(mu,sigma); +} + +generated quantities { + real alpha; + real beta1; + real beta3; + + alpha = alpha_std - (beta1_std*bar_x1)/x1_sd + + (beta3_std*bar_x1^2)/x1_sd^2; + + beta1 = beta1_std/x1_sd - 2*(beta3_std*bar_x1)/x1_sd^2; + + beta3 = beta3_std/x1_sd^2; +}" + +data_m4.6 <- list( + N = nrow(d), + height = d$height, + weight_s = d$weight_s, + weight_s2 = d$weight_s2 +) + +fit_m4.6 <- stan(data = data_m4.6, + model_code = stan_m4.6, + chains = 4, + iter = 2000) + +``` + +```{r} +print(fit_m4.6, pars = c("alpha","beta1", "beta3", "sigma")) +print(fit_m4.6, pars = c("alpha_std","beta1_std", "beta3_std", "sigma")) +precis(m4.5) +``` + +```{r} +traceplot(fit_m4.6, pars = "sigma") +``` + +```{r} +sigma_sample <- extract(fit_m4.6)[["sigma"]] +hist(sigma_sample) +``` diff --git a/.Rproj.user/B584AE7E/sources/per/t/AC03FAA1 b/.Rproj.user/B584AE7E/sources/per/t/AC03FAA1 new file mode 100644 index 0000000..47801f5 --- /dev/null +++ b/.Rproj.user/B584AE7E/sources/per/t/AC03FAA1 @@ -0,0 +1,28 @@ +{ + "id": "AC03FAA1", + "path": null, + "project_path": null, + "type": "object_explorer", + "hash": "0", + "contents": "", + "dirty": false, + "created": 1674957271066.0, + "source_on_save": false, + "relative_order": 4, + "properties": { + "id": "4e0121cfc9e74692bb952169ff077b78", + "name": "data_m4.5", + "title": "data_m4.5", + "language": "R", + "source_window_id": "", + "Source": "Source" + }, + "folds": "", + "lastKnownWriteTime": 8462582696615503216, + "encoding": "", + "collab_server": "", + "source_window": "", + "last_content_update": 1674957271066, + "read_only": false, + "read_only_alternatives": [] +} \ No newline at end of file diff --git a/.Rproj.user/B584AE7E/sources/per/t/AC03FAA1-contents b/.Rproj.user/B584AE7E/sources/per/t/AC03FAA1-contents new file mode 100644 index 0000000..e69de29 diff --git a/.Rproj.user/B584AE7E/sources/per/t/FA83A758 b/.Rproj.user/B584AE7E/sources/per/t/FA83A758 new file mode 100644 index 0000000..9036849 --- /dev/null +++ b/.Rproj.user/B584AE7E/sources/per/t/FA83A758 @@ -0,0 +1,26 @@ +{ + "id": "FA83A758", + "path": "C:/Users/lacor/OneDrive/100. Personal/SelfDevelopment/20. Project 2019/Bayesian-Inference/MetaPolls_vFINAL.Rmd", + "project_path": null, + "type": "r_markdown", + "hash": "3543462037", + "contents": "", + "dirty": false, + "created": 1674958961238.0, + "source_on_save": false, + "relative_order": 5, + "properties": { + "source_window_id": "", + "Source": "Source", + "cursorPosition": "662,35", + "scrollLine": "649" + }, + "folds": "", + "lastKnownWriteTime": 1667249148, + "encoding": "UTF-8", + "collab_server": "", + "source_window": "", + "last_content_update": 1667249148, + "read_only": false, + "read_only_alternatives": [] +} \ No newline at end of file diff --git a/.Rproj.user/B584AE7E/sources/per/t/FA83A758-contents b/.Rproj.user/B584AE7E/sources/per/t/FA83A758-contents new file mode 100644 index 0000000..5c44a27 --- /dev/null +++ b/.Rproj.user/B584AE7E/sources/per/t/FA83A758-contents @@ -0,0 +1,1875 @@ +--- +title: "Meta Analysis - Polls Elections 2022" +author: "Luis Correia" +date: "10/16/2022" +output: + pdf_document: default + html_document: + df_print: paged + rmarkdown::pdf_document: + fig_caption: yes + number_sections: yes +header-includes: +- \usepackage[margin=1in]{geometry} +- \usepackage{amsmath,amsthm,amssymb,amsfonts} +- \usepackage{relsize} +- \usepackage{lscape} +- \usepackage{enumerate} +- \usepackage{enumitem} +- \usepackage{setspace} +- \usepackage{tikz} +- \usepackage{bm} +- \usepackage{bbm} +- \usepackage[utf8]{inputenc} +- \usepackage{mathtools, nccmath} +- \usepackage{fancyhdr} +- \usepackage{float} +- \usepackage{longtable} +- \usepackage{threeparttable} +- \usepackage[linesnumbered,vlined,ruled,commentsnumbered]{algorithm2e} +- \floatplacement{figure}{H} +- \floatplacement{table}{H} +- \setlength{\parskip}{1em} +- \pagestyle{fancy} +- \fancyhf{} +- \rhead{Polls Review - Meta Analysis} +- \rfoot{Page \thepage} +urlcolor: blue +--- + +```{r setup, include=FALSE, cache = FALSE} +require("knitr") + +## setting working directory +opts_chunk$set(echo = FALSE, warning=FALSE, message = FALSE) + +library(ggplot2) +library(tidyverse) +library(bayesplot) +library(dplyr) +library(rstan) +library(meta) +library(gtools) +library(kableExtra) + +# The palette with black - Used in Graphs with : +cbp1 <- c("#999999", "#E69F00", "#56B4E9", "#009E73", + "#F0E442", "#0072B2", "#D55E00", "#CC79A7") +cbp2 <- c("#000000", "#E69F00", "#56B4E9", "#009E73", + "#F0E442", "#0072B2", "#D55E00", "#CC79A7") +cbp3 <- c("#FFDB6D", "#C4961A", "#F4EDCA", "#D16103", + "#C3D7A4", "#52854C", "#4E84C4", "#293352") + +# No. of polls - db_poll_wrk %>% group_by(key) %>% summarise() %>% pull() %>% unique() %>% length() +# No. of Institutes - db_poll_wrk %>% group_by(inst) %>% summarise() %>% pull() %>% unique() %>% length() + +# Use specific institute +# target_inst = "brasmarket" +#target_inst = c("brasmarket","parana pesquisas","gerp", "verita") + +inst_bols <- c("6sigma","brasmarket", "equilibrio","gerp","verita","instituto franca", "ideia big data", "parana pesquisas") +inst_lula <- c("atlasintel/internet", "datafolha", "ipec", "ipespe", "fsb", "quaest", "real time big data") +inst_neutral <- c("mda","poderdata", "futura inteligencia", "ranking") + +# Use specific institute +target_round = 2 + +# Adjust Bias +adjust_bias <- FALSE +``` + +```{r} +pesq_data <- read.csv("data//pesquisa_eleitoral_2022_BRASIL.csv", sep=";", header=TRUE, encoding="latin1") +``` +\section{Read data from Pooling Data} + +```{r} +pollingdata <- read.csv("CleanedPollingData_Oct25th2022.csv",header=FALSE) +pdata <- tail(as.matrix(pollingdata), -9) +header_pdata <- c("inst", "code", "date", "modo","entrev","pct_lula","pct_bols","y_null") + +n <- 1 +reg_pdata <- data.frame() +while (n < dim(pdata)[1]){ + inst <- tolower(pdata[n,1]) + code <- pdata[n+1,1] + date <- as.Date(pdata[n+1,2]) + modo <- pdata[n+2,1] + entrevistas <- as.integer(pdata[n+3,1]) + pct_lula <- as.numeric(pdata[n+4,1])/100.0 + pct_bols <- as.numeric(pdata[n+5,1])/100.0 + y_null <- as.numeric(pdata[n+6,1])/100.0 + + reg_pdata = rbind(reg_pdata, data.frame( + inst = inst, + regno = code, + date = date, + type = modo, + n = entrevistas, + Lula = pct_lula, + Bolsonaro = pct_bols, + Y_Null = y_null)) + + + n <- n+7 +} +db_poll_wrk <- pivot_longer(reg_pdata,cols=Lula:Y_Null,names_to="name", values_to="pct") |> + mutate(Me = pct, + Pe = pct*n, + Ne = Pe, + Se = pct*(1-pct)*n, + Mc = (1-pct), + Pc = (1-pct)*n, + Nc = Pc, + Sc = (1-pct)*pct*n, + newlbl = ifelse(name=="Y_Null", "Y-Blank/Null", name), + key=paste0(inst,date), + y_cat = round(pct*n), + seqno = 1, + turno = 2) +remove(reg_pdata, pdata, pollingdata) + +# Compatibilize working databases + +db_work <- db_poll_wrk %>% + group_by(key, inst, date, newlbl) %>% + summarise(y = sum(y_cat)) |> + group_by(date, inst) |> + mutate(pct_val = y/sum(y)) |> + ungroup() + +paramlbl <- db_work %>% + group_by(newlbl) %>% + summarise() %>% + pull(newlbl) +``` + +```{r} +viespollingdata <- read.csv("ViesPollingData_Oct27th2022.csv",header=FALSE) +viespdata <- tail(as.matrix(viespollingdata), -9) +header_pdata <- c("inst", "code", "date", "modo","entrev","pct_lula","pct_bols","y_null") + +n <- 1 +db_viespdata <- data.frame() +while (n < dim(viespdata)[1]){ + inst <- tolower(viespdata[n]) + modo <- viespdata[n+1] + vbols <- as.numeric(viespdata[n+2]) + vlula <- as.numeric(viespdata[n+3]) + vnull <- as.numeric(viespdata[n+4]) + + db_viespdata = rbind(db_viespdata, data.frame( + inst = inst, + type = modo, + vbols = vbols, + vlula = vlula, + vnull = vnull)) + + + n <- n+5 +} +remove(viespdata, viespollingdata) + +# Apply bias reduction +if (adjust_bias) { + # Temporary join dataframe + db_jn <- left_join(db_poll_wrk, db_viespdata) + + db_poll_wrk <- db_jn |> + group_by(newlbl) |> + mutate(pct = ifelse(newlbl=="Bolsonaro",pct-vbols, + ifelse(newlbl=="Lula",pct-vlula, + ifelse(newlbl=="Y_Blank/Null",pct-vnull,pct))), + Me = pct, + Pe = pct*n, + Ne = Pe, + Se = pct*(1-pct)*n, + Mc = (1-pct), + Pc = (1-pct)*n, + Nc = Pc, + Sc = (1-pct)*pct*n, + y_cat = round(pct*n)) + + remove(db_jn) +} + +``` + +\section{Read data from PoderData} + +```{r} +polldata <- read_csv("polls_vFINAL.csv", show_col_types = FALSE) +# polldata <- read_csv("polls_2018_v0.csv", show_col_types = FALSE) + +# Recoding parameters +## We consider the valid candidates 1-(K-1) and 1 category of blank votes + +#polldata |> +# group_by(name) |> +# summarise(n=n()) + +other_cat <- c("branco / nulo", + "brancos", + "nulos", + "branco / nulo / indeciso", + "branco / nulo / indeciso / nenhum / nao sabe / nao respondeu", + "branco / nulo / nao votara", + "branco / nulo / nenhum", + "branco / nulo / ninguem / justificar") +undecided_cat <- c("indeciso", + "indeciso / nao sabe / nao respondeu", + "nao respondeu", + "nao sabe", + "nao sabe / nao respondeu", + "nao sabem", + "nenhum", + "nenhum / nao sabe / nao respondeu", + "Outros") +valid_candidates <- c("Bolsonaro", "Ciro", "Eymael", "Leonardo Pericles", "Luiz Felipe d'Avila", + "Lula","Padre Kelmon","Pericles", + # "Roberto Jefferson", "Pablo Marcal", + "Simone Tebet", "Sofia Manzano", "Soraya Thronicke", "Vera Lucia", + "Vera Lucia Salgado") + +# actual_candidates <- c("Bolsonaro", "Haddad", "Ciro", "Marina", "Alckmin") # 2018 + +# Threshold To consider relevant candidate +threshold_pct <- .02 + +db_poll_wrk <- polldata %>% + group_by(name) %>% + mutate (Me = pct, + Pe = pct*n, + Ne = Pe, + Se = pct*(1-pct)*n, + Mc = (1-pct), + Pc = (1-pct)*n, + Nc = Pc, + Sc = (1-pct)*pct*n, + newlbl = ifelse(is.element(name, other_cat), "Y-Blank/Null", + ifelse(is.element(name,undecided_cat),"Z-Undec", + ifelse(!is.element(name,valid_candidates), "Y-Blank/Null", + ifelse(pct>threshold_pct, name, "X-Other")))), + + y_cat = round(pct*n)) + + +``` + +\subsection{Calculate and Adjust bias from results of 1st Round} + +Let $$Y_k$$ be the bias from the $$k^{th}$$ institute we can estimate this bias by calculating the difference between the estimate for 1st round and the final results of the elections on October 2nd. + +```{r, message=FALSE} +# Calculate bias from 1st round and apply to 2nd round +db_urnas <- db_poll_wrk |> + filter(turno==2, inst=="urnas") |> + ungroup() + +# Select polls from 1st round +db_tmp0.a <- db_poll_wrk |> + filter(turno==2, inst!="urnas", pct > 0.0) |> # , newlbl!="Z-Undec" + select(inst,type,name, newlbl,pct) |> + left_join(db_urnas, by="name") |> + mutate(error_pct = pct.x-pct.y, + relerror_pct = error_pct/pct.y, + ratio_pct = pct.y/pct.x) |> + filter(newlbl.x != "Y-Blank/Null", newlbl.x != "Z-Undec") |> + select(inst.x, type.x, name, newlbl.x,pct.x,pct.y,error_pct,relerror_pct,ratio_pct) + +db_tmp0.b <- db_tmp0.a |> + group_by(inst.x, type.x) |> + mutate(n_inst = n(), + SD_pct = sd(error_pct), + MD_pct = sum(abs(error_pct))/n_inst, + RMSE_pct = sqrt(sum(error_pct^2)/n_inst), + Dev_lower = MD_pct - qnorm(.975)*SD_pct/sqrt(n_inst), + Dev_upper = MD_pct + qnorm(.975)*SD_pct/sqrt(n_inst)) |> + ungroup() + +db_inst <- rbind(data.frame( + inst.x = inst_lula, + tend = rep("Lula",length(inst_lula)) +), +data.frame( + inst.x = inst_bols, + tend = rep("Bolsonaro",length(inst_bols)) +), +data.frame( + inst.x = inst_neutral, + tend = rep("Neutral",length(inst_neutral)) +)) + +db_tmp0.b |> + group_by(inst.x) |> + summarise(Dev_lower, MD_pct, Dev_upper, RMSE_pct) |> + unique() |> left_join(db_inst,by="inst.x") |> + ggplot(aes(x=inst.x))+ + geom_point(aes(y=RMSE_pct),stat = "identity",size=3.5,shape="diamond")+ + geom_point(aes(y=MD_pct))+ #, color=tend))+ + geom_errorbar(aes(ymin=Dev_lower, ymax=Dev_upper, color=tend))+ + labs(x="Institute", y="Mean Deviation", title="Brazilian Elections - Deviation Research Institutes in 2nd Round")+ + scale_color_manual(breaks = c("Lula", "Bolsonaro", "Neutral"), + values=c("red", "blue", "grey"))+ + theme(axis.text.x = element_text(angle = 90, vjust = 0)) + +# Select Target Institutes with lower bias/variance + +# Identify sets of institutes +inst_null <- c() + +inst_all <- db_tmp0.b |> + select(inst.x, MD_pct, SD_pct) |> + arrange(MD_pct,SD_pct) |> + unique() |> + pull(inst.x) + +inst_le.2 <- db_tmp0.b |> + select(inst.x, MD_pct, SD_pct) |> + arrange(MD_pct,SD_pct) |> + filter(MD_pct<.02) |> + unique() |> + pull(inst.x) + +inst_le.35 <- db_tmp0.b |> + select(inst.x, MD_pct, SD_pct) |> + arrange(MD_pct,SD_pct) |> + filter(MD_pct<.035) |> + unique() |> + pull(inst.x) + +inst_le.40 <- db_tmp0.b |> + select(inst.x, MD_pct, SD_pct) |> + arrange(MD_pct,SD_pct) |> + filter(MD_pct<.04) |> + unique() |> + pull(inst.x) + +# Select target Institutes +target_inst <- inst_null + +``` + + +```{r} +# Calculate bias from 1st round and apply to 2nd round +db_urnas <- db_poll_wrk |> + filter(turno==1, inst=="urnas") |> + ungroup() + +# Select polls from 1st round +db_tmp0.a <- db_poll_wrk |> + filter(turno==1, inst!="urnas", newlbl!="Z-Undec", pct > 0.0) |> + select(inst,type,name, newlbl,pct) + +# Select relevant fields from 'urnas' +db_tmp0.b <- db_urnas |> + group_by(newlbl) |> +# select(newlbl, pct) |> + summarise(pct = sum(pct)) + + +db_tmp1 <- left_join(db_tmp0.a, db_tmp0.b,by="newlbl")|> +# mutate(bias = pct.x-pct.y) |> + mutate(bias = pct.y/pct.x) |> # calculate relative bias from 1st round results + select(inst, type, newlbl, bias) |> + ungroup() + +db_tmp1 |> group_by(inst, type, newlbl) |> + mutate(n=n(), + bal_bias = bias/sum(bias), + norm_bias = bal_bias/sum(bal_bias)) + +#db_tmp |> filter(newlbl=="Bolsonaro",inst=="datafolha") |> +# group_by(inst,type) |> +# mutate(avg_bias = mean(bias), +# sd_bias = sd(bias), +# n_bias = n(), +# std_bias = (bias-avg_bias)/(sd_bias)) + +db_viespdata <- db_tmp1 |> + filter(newlbl %in% c("Bolsonaro", "Lula", "Y-Blank/Null")) |> + group_by(inst, type, newlbl) |> + mutate(avg_bias=mean(bias), + sd_bias=sd(bias), + n_bias = n()) |> + select(inst,type,newlbl,avg_bias,sd_bias,n_bias) |> + unique() |> +# group_by(inst,type) |> + pivot_wider(id_cols=inst:type, names_from=newlbl, values_from = avg_bias:n_bias) |> + mutate(vbols=avg_bias_Bolsonaro, + vlula=avg_bias_Lula, + vnull=`avg_bias_Y-Blank/Null`) |> + select(inst,type,vbols, vlula, vnull) + + +#db_viespdata <- db_tmp1 |> +# filter(newlbl %in% c("Bolsonaro", "Lula", "Y-Blank/Null")) |> +# group_by(inst, type, newlbl) |> +# mutate(avg_bias=mean(bias), +# sd_bias=sd(bias), +# n_bias = n(), +# std_bias = (bias-avg_bias)/(sd_bias)) |> +# select(inst,type,newlbl,avg_bias,sd_bias,n_bias,std_bias) |> +# unique() |> +## group_by(inst,type) |> +# pivot_wider(id_cols=inst:type, names_from=newlbl, values_from = avg_bias:std_bias) |> +# mutate(vbols=avg_bias_Bolsonaro, +# vlula=avg_bias_Lula, +# vnull=`avg_bias_Y-Blank/Null`) |> +# select(inst,type,vbols, vlula, vnull) + +remove(db_tmp0.a, db_tmp0.b, db_tmp1, db_urnas) + +# Apply bias reduction +if (adjust_bias) { + # Temporary join dataframe + db_jn <- left_join(db_poll_wrk, db_viespdata) + + db_poll_wrk <- db_jn |> + group_by(newlbl) |> + mutate(pct = ifelse(newlbl=="Bolsonaro",pct*vbols, + ifelse(newlbl=="Lula",pct*vlula, + ifelse(newlbl=="Y_Blank/Null",pct*vnull,pct))), + Me = pct, + Pe = pct*n, + Ne = Pe, + Se = pct*(1-pct)*n, + Mc = (1-pct), + Pc = (1-pct)*n, + Nc = Pc, + Sc = (1-pct)*pct*n, + y_cat = round(pct*n)) + + remove(db_jn) +} + +``` +\section{Elaborate dataframe for processing (both cases)} + +```{r, message=FALSE} +# Restrict analysis for Scenario #1 +db_work <- db_poll_wrk %>% + filter (seqno==1, turno==target_round) %>% + group_by(key, inst, date, newlbl) %>% + summarise(y = sum(y_cat)) |> + group_by(date, inst) |> +# filter(newlbl != "Z-Null") |> + filter(newlbl != "Z-Undec") |> + mutate(pct_val = y/sum(y)) |> + ungroup() + +paramlbl <- db_work %>% + group_by(newlbl) %>% + summarise() %>% + pull(newlbl) +``` + + +```{r} +# Loading ’meta’ package (version 4.0-2). +# 2. Do meta-analysis +start_date <- "2022-10-02" +#start_date = "2022-04-03" +end_date <- tail(db_poll_wrk |> group_by(date) |> summarise() |> pull(),1) + +# if (target_inst=="") { +if (length(target_inst)==0) { + mpL <- metaprop(Ne, n, studlab=key, method = "Inverse", method.ci = "NAsm", + data=db_poll_wrk %>% filter (newlbl=="Lula", seqno==1, turno==2, date >= start_date, date <= end_date)) + if (target_round==1) { + mpC <- metaprop(Ne, n, studlab=key, method = "Inverse", method.ci = "NAsm", + data=db_poll_wrk %>% filter (newlbl=="Ciro", seqno==1, date >= start_date, date <= end_date)) + } + mpB <- metaprop(Ne, n, studlab=key, method = "Inverse", method.ci = "NAsm", + data=db_poll_wrk %>% filter (newlbl=="Bolsonaro", seqno==1, turno==2, date >= start_date, date <= end_date)) + if (target_round==1) { + mpS <- metaprop(Ne, n, studlab=key, method = "Inverse", method.ci = "NAsm", + data=db_poll_wrk %>% filter (newlbl=="Simone Tebet", seqno==1, date >= start_date, date <= end_date)) + } +} else { + mpL <- metaprop(Ne, n, studlab=key, method = "Inverse", method.ci = "NAsm", + data=db_poll_wrk %>% filter (newlbl=="Lula", seqno==1, date >= start_date, date <= end_date, inst %in% target_inst)) + if (target_round==1) { + mpC <- metaprop(Ne, n, studlab=key, method = "Inverse", method.ci = "NAsm", + data=db_poll_wrk %>% filter (newlbl=="Ciro", seqno==1, date >= start_date, date <= end_date, inst %in% target_inst)) + } + mpB <- metaprop(Ne, n, studlab=key, method = "Inverse", method.ci = "NAsm", + data=db_poll_wrk %>% filter (newlbl=="Bolsonaro", seqno==1, date >= start_date, date <= end_date, inst %in% target_inst)) + if (target_round==1) { + mpS <- metaprop(Ne, n, studlab=key, method = "Inverse", method.ci = "NAsm", + data=db_poll_wrk %>% filter (newlbl=="Simone Tebet", seqno==1, date >= start_date, date <= end_date, inst %in% target_inst)) + } +} + +``` + +```{r, fig.height=12, fig.width=10} +meta_bolsonaro <- forest(mpB, xlab="Bolsonaro") +if (target_round==1) forest(mpC, xlab="Ciro") +meta_lula <- forest(mpL, xlab="Lula") +if (target_round==1) forest(mpS, xlab="Simone Tebet") +``` + + +```{r} +#db_work %>% ggplot(aes(x=date, y=y, col=newlbl))+ +# geom_line(size = 0.8) + +# ggtitle("Evolution") + +# xlab("Date") + ylab("% Voters")+ +# theme_bw() +``` + +```{r} +# Elections 2018 +#start_date = "2018-09-10" + +db_work_obs <- pivot_wider(db_work, id_cols = c("key","inst","date"), values_from = "y", names_from = "newlbl") %>% + arrange(date) +db_work_obs[is.na(db_work_obs)] <- 0 + +if (length(target_inst)==0) { + y <- as.integer(unname(as.matrix(db_work_obs %>% filter(date >= start_date, date <= end_date))[,paramlbl])) +} else { + y <- as.integer(unname(as.matrix(db_work_obs %>% filter(date >= start_date, date <= end_date, inst %in% target_inst))[,paramlbl])) +} + +y <- matrix(y,ncol=length(paramlbl),byrow=FALSE) +y_sums <- rowSums(y) +``` + +STAN - Multinomial + +```{r} +#https://vasishth.github.io/bayescogsci/book/modeling-multiple-categorical-responses.html +# Why using target +# https://stackoverflow.com/questions/40289457/stan-using-target-syntax +# https://discourse.mc-stan.org/t/differences-between-normal-and-target-normal-lpdf/3519 + + +m2.2 <- " +data { + int N; + int K; + vector[N] N_trials; + vector[K] alpha; + array[N, K] int ans; +} +parameters { + simplex[K] theta; +} +model { + target += dirichlet_lpdf(theta | alpha); // rep_vector(2, 5)); + for (n in 1:N) { + target += multinomial_lpmf(ans[n] | theta); + } +} +generated quantities{ + int pred_ans[K] = multinomial_rng(theta, K); +}" + +alpha_mean <- y[1,] +#alpha_mean <- c(7,3,8,1,2,3) +#alpha_mean <- c(3.5,1.5,2,.5,1,1.5) +#alpha_mean <- rep(1,6) + +data_mn.2 <- list(N = dim(y)[1], + K = dim(y)[2], + N_trials = y_sums, +# alpha = rep(3,dim(y)[2]), + alpha = alpha_mean, + ans = y) +``` + +Interpretation of the Alphas in the Dirichlet distribution + +https://stats.stackexchange.com/questions/244917/what-exactly-is-the-alpha-in-the-dirichlet-distribution + +https://stats.stackexchange.com/questions/145705/can-a-multinomial1-n-1-n-be-characterized-as-a-discretized-dirichlet1 + + +Beta Distribution (uni-dimensional case) +https://stats.stackexchange.com/questions/243878/conjugate-beta-interpretation-of-the-continuous-binomial-signal + +https://stats.stackexchange.com/questions/44494/why-is-the-dirichlet-distribution-the-prior-for-the-multinomial-distribution + + + +```{r} +m2.3 <- " +data { + int N; + int K; + int AMax; +// vector[N] N_trials; + array[N, K] int ans; +} +parameters { + vector[K] alpha; + simplex[K] theta; +} + +model { + + alpha ~ normal(250,40); // (150,30) + + target += dirichlet_lpdf(theta | alpha); // rep_vector(2, 5)); + for (n in 1:N) { + target += multinomial_lpmf(ans[n] | theta); + } +} +generated quantities{ + int pred_ans[K] = multinomial_rng(theta, K); +}" +AMax_Value <- 380 + +data_mn.3 <- list(N = dim(y)[1], + K = dim(y)[2], + AMax = AMax_Value, +# N_trials = y_sums, + ans = y) +``` + + +```{r} +#fit_mn.2 <- stan(data = data_mn.2, +# model_code = m2.2, +# iter = 1500, +# seed = 530, +# verbose=TRUE) + +fit_mn.3 <- stan(data = data_mn.3, + model_code = m2.3, + iter = 2200, + seed = 530, + verbose=TRUE) + +# Select Model +MODEL_SELECTED <- 3 +fit_mn <- fit_mn.3 + +``` + +```{r} +if (MODEL_SELECTED == 3) + print(fit_mn, pars = c("alpha")) + +print(fit_mn, pars = c("theta")) +print(paramlbl) + +``` + +```{r} +theta <- extract(fit_mn)[["theta"]] +theta_mean <- colMeans(theta) + +if (MODEL_SELECTED == 3) { + alpha <- extract(fit_mn)[["alpha"]] + alpha_mean <- colMeans(alpha) +} + +yrep <- extract(fit_mn)[["pred_ans"]] +#log_lik <- extract(fit_mn)[["target"]] +nsim<- nrow(yrep) +``` + + +```{r} +m1 <- mean(theta[,1]); s1 <- sd(theta[,1]) +m2 <- mean(theta[,2]); s2 <- sd(theta[,2]) +cat("\n",format(paramlbl[1], digits=10),": mean=",format(m1,digits=3,nsmall=1),"and sd=",format(s1,digits=4,nsmall=2 )) +cat("\n",format(paramlbl[2], digits=10),": mean=",format(m2,digits=3,nsmall=1),"and sd=",format(s2,digits=4,nsmall=2 )) +``` + + +```{r} +#traceplot(fit_mn, pars = "alpha") +``` + +```{r} +# Elections 2022 1st. Round +if (target_round==1) { + p1 <- 1 + p2 <- 3 +} else { +# Elections 2022 - 2nd Round + p1 <- 1 + p2 <- 2 +} +# Elections 2018 +#p1 <- 2 +#p2 <- 4 + +m1 <- mean(theta[,p1]) +m2 <- mean(theta[,p2]) +b_vs_l <- stan_scat(fit_mn, pars=c(paste0("theta[", c(p1,p2), "]"))) +b_vs_l + + geom_vline(xintercept = m1, colour="blue")+ + geom_hline(yintercept = m2, colour="red")+ + xlab(paramlbl[p1])+ylab(paramlbl[p2])+ + theme_bw() +``` + + +```{r} +stan_hist(fit_mn, pars=c(paste0("theta[",1:3,"]")), bins=30) + +if (MODEL_SELECTED == 3) + stan_hist(fit_mn, pars=c(paste0("alpha[",1:3,"]")), bins=30) +``` + +```{r} +if (MODEL_SELECTED == 3) + stan_dens(fit_mn, pars = "alpha") +``` + + +```{r} +stan_dens(fit_mn, pars = "theta") +``` + +```{r, warning=FALSE} +panel.cor <- function(x, y, digits = 2, prefix = "", cex.cor, ...) +{ + par(usr = c(0, 1, 0, 1)) + r <- abs(cor(x, y)) + txt <- format(c(r, 0.123456789), digits = digits)[1] + txt <- paste0(prefix, txt) + if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt) + text(0.5, 0.5, txt, cex = cex.cor * r) +} +pairs(fit_mn, pars = c("theta"),lower.panel = panel.smooth, upper.panel = panel.cor, + labels = paramlbl, gap=0, row1attop=FALSE) +``` + +```{r} +stan_rhat(fit_mn, bins=30) +``` + +```{r} +samp_info <- stan_diag(fit_mn, info='sample') +``` + +```{r} +samp_info[[3]] +``` + + +```{r} +stan_mcse(fit_mn, pars="theta", bins=30) +``` + + +```{r} +mcmc_trace(As.mcmc.list(fit_mn, pars="theta")) +``` + +CALCULATE exact probability using Numeric Integration + +```{r} +Fmax_ij <- function (u, i, j, alpha, betta) { + p <- 1.0 + for (k in c(1:(length(alpha)-1))[-c(i,j)]){ + p <- p*pgamma(u, alpha[k], betta) + } + #cat("\n\nFMax -> p=",p," c()=",c(1:(length(alpha)-1))[-c(i,j)]," alpha=",alpha," betta=",betta) + return (p) +} + +fmin_ij <- function (u, i, j, alpha, betta) { + p1 <- dgamma(u,alpha[i],betta)*(1-pgamma(u,alpha[j],betta)) + p2 <- dgamma(u,alpha[j],betta)*(1-pgamma(u,alpha[i],betta)) + #cat("\nfmin -> p1=",p1,"/ p2=",p2," alpha=",alpha," betta=",betta) + return (p1+p2) +} + +Fsum_ij <- function (u, i, alpha, betta) { + #cat("\n\n[Call]Fsum_ij -> u=",u,"/ i=",i," alpha=",alpha," betta=",betta," sum=",sum(alpha[-c(i,length(alpha))])) + #cat("\n--> return value=",pgamma(u, sum(alpha[-c(i,length(alpha))]), betta)) + return(pgamma(u, sum(alpha[-c(i,length(alpha))]), betta)) +} + +fsum_ij <- function (u, i, alpha, betta) { + return(dgamma(u, sum(alpha[-c(i,length(alpha))]), betta)) +} + +gammasum <- function (u, i, alpha, betta) { + #cat("\n\n[Call]gammasum -> u=",u,"/ i=",i," alpha=",alpha," betta=",betta) + #cat("\n--> dgamma=",dgamma(u, alpha[i], betta)) + return (Fsum_ij(u, i, alpha, betta) * dgamma(u, alpha[i], betta)) +} + +gamma_ij <- function(u, i, j, alpha, betta) { +# cat("\ngamma_ij -> u=",u,"/ (i,j)=(",i,",",j,") alpha=",alpha," betta=",betta,"\n\n") + return(pgamma(u, alpha[j], betta)*dgamma(u, alpha[i], betta)) +} + +ddelta_ij <- function (u, i, j, alpha, betta) { + return (Fmax_ij(u, i, j, alpha, betta) * fmin_ij(u, i, j, alpha, betta)) +} +``` + + +```{r} +# Iterative process to calculate the probabilities +# Probability of C1 winning in the first round + +``` + +RUN SCenarios based on STAN Model + +\subsection{Probabilities of Round 1} + +```{r} +# Scenarios Setup +if (MODEL_SELECTED == 3) { + alph <- alpha_mean +} else { + alph <- c(7.32,2.29,7.78,1.92,1.24,2.21) +} + +beth <- 1.0 +lim_sup <- 20 + +#2022 +c1 <- 1 +c2 <- 3 + +# Calculate the probability of exact candidates c1 and c2 pass to 2nd round +P.c1c2 <- MASS::area(ddelta_ij, a=0, b=lim_sup, c1, c2, alph, beth, + fa=0, fb=ddelta_ij(lim_sup, c1, c2, alph, beth), + limit = 10, eps = 1e-05) + +# Calculate the probability of candidate c1 pass to 2nd round +P2ndround <- 0.0 +for (k in c(1:(length(alph)-1))[-c(c1)]) { + # Probability of Candidates c1 and Candidate k pass 1st. round + P1.ij <- MASS::area(ddelta_ij, a=0, b=lim_sup, c1, k, alph, beth, + fa=0, fb=ddelta_ij(lim_sup, c1, k, alph, beth), + limit = 10, eps = 1e-05) + + # Probability of Candidates c1 overcome Candidate k in 2nd. round + P3.ij <- MASS::area(gamma_ij, a=0, b=lim_sup, c1, k, alph, beth, + fa=0, fb=gamma_ij(lim_sup, c1, k, alph, beth), + limit = 10, eps = 1e-05) + P2ndround <- P2ndround + P1.ij * P3.ij + +} + +# Probability of candidate c1 win in 1st round +P2.i <- MASS::area(gammasum, a=0, b=lim_sup, c1, alph, beth, + fa=0, fb=gammasum(lim_sup, c1, alph, beth), + limit = 10, eps = 1e-05) + +# Probability of None wins in 1st round +SumP2 <- 0.0 +for (k in c(1:(length(alph)-1))) { + # Probability of Candidates k wins in 1st. round + P.ck <- MASS::area(gammasum, a=0, b=lim_sup, k, alph, beth, + fa=0, fb=gammasum(lim_sup, k, alph, beth), + limit = 10, eps = 1e-05) + cat("\nProbability of candidate",paramlbl[k],"win in 1st round", P.ck,"\n") + SumP2 <- SumP2 + P.ck + +} + +# Probability of candidate c2 win in 1st round +#P2.j <- MASS::area(gammasum, a=0, b=lim_sup, c2, alph, beth, +# fa=0, fb=gammasum(lim_sup, c2, alph, beth), +# limit = 10, eps = 1e-05) + +# Probability of candidate c1 win elections +totProb.ij <- P2.i + (1-SumP2) * P2ndround # P1.ij * P3.ij + +# Show Results of projected Scenario +cat("\n==> Probability of NO CANDIDATE is elected in 1st round ", (1-SumP2)) +cat("\n\nProbability of scenario with candidate",paramlbl[c1],"pass 1st round", P2ndround) +cat("\nProbability of candidate",paramlbl[c1],"and",paramlbl[c2],"are the most voted in 1st Round", P.c1c2) +# cat("\nProbability of candidate ",paramlbl[c1]," win in 1st round", P2.i) +# cat("\nProbability of candidate ",paramlbl[c2]," win in 1st round", P2.j) +# cat("\nProbability of candidate ",paramlbl[c1]," overcome candidate ",paramlbl[c2]," in 2nd round", P3.ij) +cat("\nIn this scenario, the probability of candidate",paramlbl[c1],"win Elections is", totProb.ij) +``` +All possible scenarios in round 1 + +```{r} +for (i in 1:5) { + cat("\nScenarios with w/ C1:",toupper(paramlbl[i])) + S <- 0.0 + for (j in c(1:5)[-i]) { + P.ij <- MASS::area(ddelta_ij, a=0, b=lim_sup, i, j, alph, beth, + fa=0, fb=ddelta_ij(lim_sup, i, j, alph, beth), + limit = 10, eps = 1e-05) + cat("\n... and ",paramlbl[j],"are the most voted in 1st Round", + format(P.ij, width = 8, digits = 2, + nsmall = 6, justify = "right")) + S <- S + P.ij + } + cat("\nTotal Probability of",toupper(paramlbl[i]), " -> ", + format(round(S, digits=4), width = 6, digits = 2, + nsmall = 4, justify = "right")) + cat("\n---------------------------\n") +} +``` + +\subsection{Probabilities of Round 2} + +```{r} +# Scenarios Setup +if (MODEL_SELECTED == 3) { + alph <- alpha_mean +} else { + alph <- c(7.32,2.29,7.78,1.92,1.24,2.21) +} + +beth <- 1.0 +lim_sup <- AMax_Value + +#2022 +c1 <- 1 +c2 <- 2 + +# Calculate the probability of candidate c1 pass to 2nd round +#PWin2ndround <- MASS::area(gamma_ij, a=0, b=lim_sup, c1, c2, alph, beth, +# fa=0, fb=gamma_ij(lim_sup, c1, c2, alph, beth), +# limit = 10, eps = 1e-05) +#MASS::area(gamma_ij, a=0, b=lim_sup, c1, c2, alph, beth, +# fa=0, fb=gamma_ij(lim_sup, c1, c2, alph, beth), +# limit = 10, eps = 1e-05) + +#PC1Win2ndround <- integrate(gamma_ij,lower = 0, upper = lim_sup, i=c1, j=c2, alpha=alph, betta=beth)$value +#PC2Win2ndround <- integrate(gamma_ij,lower = 0, upper = lim_sup, i=c2, j=c1, alpha=alph, betta=beth)$value + +PC1Win2ndround <- integrate(gammasum,lower = 0, upper = lim_sup, i=c1, alpha=alph, betta=beth)$value +PC2Win2ndround <- integrate(gammasum,lower = 0, upper = lim_sup, i=c2, alpha=alph, betta=beth)$value + +# plot(dgamma(seq(0, 1000, by = 1),alph[c1], beth)*pgamma(seq(0, 1000, by = 1),alph[c2], beth)) + +# Show Results of projected Scenario +cat("\n\nProbability of",paramlbl[c1],"win elections", PC1Win2ndround) +cat("\n\nProbability of",paramlbl[c2],"win elections", PC2Win2ndround) +``` + + +Summary of Researches available + +```{r} +db_poll_wrk %>% + filter(date>=start_date) |> + group_by(regno, inst) %>% + distinct(regno) %>% + group_by(inst) %>% + summarise(n=n()) %>% + kbl(booktabs = TRUE, digits = 4, + caption = "No. of Election Polls by Institute") %>% + kable_styling(latex_options = "striped") +``` + +STAN - Build Database for Inference + +```{r} +#INitialization +# Design of Scenarios + +if (target_round==1) { + Scenarios <- data.frame ( + C1 = c(1, 1, 2), + C2 = c(3, 2, 3) + ) +} else { + Scenarios <- data.frame ( + C1 = c(1), + C2 = c(2) + ) +} + +# Scenarios 2018 +#Scenarios <- data.frame ( +# C1 = c(3, 4, 1, 2), +# C2 = c(2, 2, 2, 5) +#) + +db_iter_stats <- data.frame() + +# Prepare +if (length(target_inst)==0) { + db_iter_wrk <- db_work_obs %>% + arrange(date) %>% + filter(date >= start_date, date <= end_date) +} else { + db_iter_wrk <- db_work_obs %>% + arrange(date) %>% + filter(inst %in% target_inst, date >= start_date, date <= end_date) +} + +v_dates <- db_iter_wrk %>% group_by(date) %>% pull(date) %>% unique() + +#Initialize model variables +alph <- alpha_mean +beth <- 1.0 +#lim_sup <- 20 + +Model <- 3 + +``` + + +```{r} +# Run all scenarios +for (ScenIdx in 1:nrow(Scenarios)) { + + c1 <- Scenarios$C1[ScenIdx] + c2 <- Scenarios$C2[ScenIdx] + + cat("\nRunning Scenario ", ScenIdx, "with ", paramlbl[c1]," and ",paramlbl[c2]) + + idx <- 1 + + tb_alphas <- matrix(rep(0, length(v_dates)*length(paramlbl)), nrow=length(v_dates), ncol=length(paramlbl)) + Prob_c1c2_pass_2ndRnd <- rep(0, length(v_dates)) + Prob_c1_pass_2ndRnd <- rep(0, length(v_dates)) + Prob_c2_pass_2ndRnd <- rep(0, length(v_dates)) + Prob_c1_Win_1stRnd <- rep(0, length(v_dates)) + Prob_c2_Win_1stRnd <- rep(0, length(v_dates)) + Prob_c1_Win_2ndRnd <- rep(0, length(v_dates)) + Prob_c2_Win_2ndRnd <- rep(0, length(v_dates)) + Prob_None_Wins_1stRnd <- rep(0, length(v_dates)) + Prob_c1_wins_Elections <- rep(0, length(v_dates)) + Prob_c2_wins_Elections <- rep(0, length(v_dates)) + ProbInteg_c1_wins_Elections <- rep(0, length(v_dates)) + ProbInteg_c2_wins_Elections <- rep(0, length(v_dates)) + + while (idx <= length(v_dates)) { # Process Incremental Dates + cat(paste0("\nProcessing date ",v_dates[idx],"...\n")) + y <- as.integer(unname(as.matrix(db_iter_wrk %>% filter(date <= v_dates[idx]))[,paramlbl])) + y <- matrix(y,ncol=length(paramlbl),byrow=FALSE) + y_sums <- rowSums(y) + + # Input Data for Stan + # alpha_mean <- c(7,3,8,1,2,3) + if (Model == 2) { + data_iter_mn <- list(N = dim(y)[1], + K = dim(y)[2], + N_trials = y_sums, + alpha = alpha_mean, + ans = y) + m_code <- m2.2 + } + else { + data_iter_mn <- list(N = dim(y)[1], + K = dim(y)[2], +# AMax = 10, + AMax = AMax_Value, + ans = y) + m_code <- m2.3 + } + + # Run Stan Model + fit_iter_mn <- stan(data = data_iter_mn, + model_code = m_code, +# iter = 1500, + iter = 2200, + seed = 530, + refresh = 0) + + # Collect STAN results + theta <- extract(fit_iter_mn)[["theta"]] + theta_mean <- colMeans(theta) + + if (Model == 3) { + alpha <- extract(fit_iter_mn)[["alpha"]] + alpha_mean <- colMeans(alpha) + alph <- alpha_mean + } + + # Calculate Probabilities + cat("\nCalculating Probabilities of this scenario...") + if (target_round==1) { + # Calculate the probability of exact candidates c1 and c2 pass to 2nd round + P.c1c2 <- MASS::area(ddelta_ij, a=0, b=lim_sup, c1, c2, alph, beth, + fa=0, fb=ddelta_ij(lim_sup, c1, c2, alph, beth), + limit = 10, eps = 1e-05) + # Calculate the probability of candidate c1 pass to 2nd round + P2ndround_c1 <- 0.0 + for (k in c(1:(length(alph)-1))[-c(c1)]) { + # Probability of Candidates c1 and Candidate k pass 1st. round + P1.ij <- MASS::area(ddelta_ij, a=0, b=lim_sup, c1, k, alph, beth, + fa=0, fb=ddelta_ij(lim_sup, c1, k, alph, beth), + limit = 10, eps = 1e-05) + + # Probability of Candidates c1 overcome Candidate k in 2nd. round + P3.ij <- MASS::area(gamma_ij, a=0, b=lim_sup, c1, k, alph, beth, + fa=0, fb=gamma_ij(lim_sup, c1, k, alph, beth), + limit = 10, eps = 1e-05) + P2ndround_c1 <- P2ndround_c1 + P1.ij * P3.ij + + } + P2ndround_c2 <- 0.0 + for (k in c(1:(length(alph)-1))[-c(c2)]) { + # Probability of Candidates c2 and Candidate k pass 1st. round + P1.ji <- MASS::area(ddelta_ij, a=0, b=lim_sup, c2, k, alph, beth, + fa=0, fb=ddelta_ij(lim_sup, c2, k, alph, beth), + limit = 10, eps = 1e-05) + + # Probability of Candidates c1 overcome Candidate k in 2nd. round + P3.ji <- MASS::area(gamma_ij, a=0, b=lim_sup, c2, k, alph, beth, + fa=0, fb=gamma_ij(lim_sup, c2, k, alph, beth), + limit = 10, eps = 1e-05) + P2ndround_c2 <- P2ndround_c2 + P1.ji * P3.ji + + } + + # Probability of candidate c1 win in 1st round + P2.i <- MASS::area(gammasum, a=0, b=lim_sup, c1, alph, beth, + fa=0, fb=gammasum(lim_sup, c1, alph, beth), + limit = 10, eps = 1e-05) + + # Probability of candidate c2 win in 1st round + P2.j <- MASS::area(gammasum, a=0, b=lim_sup, c2, alph, beth, + fa=0, fb=gammasum(lim_sup, c2, alph, beth), + limit = 10, eps = 1e-05) + + # Probability of None wins in 1st round + SumP2 <- 0.0 + for (k in c(1:(length(alph)-1))) { + # Probability of Candidates k wins in 1st. round + P.ck <- MASS::area(gammasum, a=0, b=lim_sup, k, alph, beth, + fa=0, fb=gammasum(lim_sup, k, alph, beth), + limit = 10, eps = 1e-05) + # cat("\nProbability of candidate",paramlbl[k],"win in 1st round", P.ck,"\n") + SumP2 <- SumP2 + P.ck + + } + # Probability of candidate c1 win elections + totProb.ij <- P2.i + (1-SumP2) * P2ndround_c1 # P1.ij * P3.ij + + # Probability of candidate c1 win elections + totProb.ji <- P2.j + (1-SumP2) * P2ndround_c2 # P1.ij * P3.ij + } else { +# P2ndround_c1 <- 0.0 +# for (k in c(1:(length(alph)-1))[-c(c1)]) { +# # Probability of Candidates c1 and Candidate k pass 1st. round +# P1.ij <- MASS::area(ddelta_ij, a=0, b=lim_sup, c1, k, alph, beth, +# fa=0, fb=ddelta_ij(lim_sup, c1, k, alph, beth), +# limit = 10, eps = 1e-05) +# +# # Probability of Candidates c1 overcome Candidate k in 2nd. round +# P3.ij <- MASS::area(gamma_ij, a=0, b=lim_sup, c1, k, alph, beth, +# fa=0, fb=gamma_ij(lim_sup, c1, k, alph, beth), +# limit = 10, eps = 1e-05) +# P2ndround_c1 <- P2ndround_c1 + P1.ij * P3.ij +# +# } +# P2ndround_c2 <- 0.0 +# for (k in c(1:(length(alph)-1))[-c(c2)]) { +# # Probability of Candidates c2 and Candidate k pass 1st. round +# P1.ji <- MASS::area(ddelta_ij, a=0, b=lim_sup, c2, k, alph, beth, +# fa=0, fb=ddelta_ij(lim_sup, c2, k, alph, beth), +# limit = 10, eps = 1e-05) +# +# # Probability of Candidates c1 overcome Candidate k in 2nd. round +# P3.ji <- MASS::area(gamma_ij, a=0, b=lim_sup, c2, k, alph, beth, +# fa=0, fb=gamma_ij(lim_sup, c2, k, alph, beth), +# limit = 10, eps = 1e-05) +# P2ndround_c2 <- P2ndround_c2 + P1.ji * P3.ji +# +# } +# # Probability of candidate c1 win in 1st round +# P2.i <- MASS::area(gammasum, a=0, b=lim_sup, c1, alph, beth, +# fa=0, fb=gammasum(lim_sup, c1, alph, beth), +# limit = 10, eps = 1e-05) +# +# # Probability of candidate c2 win in 1st round +# P2.j <- MASS::area(gammasum, a=0, b=lim_sup, c2, alph, beth, +# fa=0, fb=gammasum(lim_sup, c2, alph, beth), +# limit = 10, eps = 1e-05) +# +# # Probability of None wins in 1st round +# SumP2 <- 0.0 +# for (k in c(1:(length(alph)-1))) { +# # Probability of Candidates k wins in 1st. round +# P.ck <- MASS::area(gammasum, a=0, b=lim_sup, k, alph, beth, +# fa=0, fb=gammasum(lim_sup, k, alph, beth), +# limit = 10, eps = 1e-05) +# # cat("\nProbability of candidate",paramlbl[k],"win in 1st round", P.ck,"\n") +# SumP2 <- SumP2 + P.ck +# +# } +# # Probability of candidate c1 win elections +# totProb.ij <- P2.i + (1-SumP2) * P2ndround_c1 # P1.ij * P3.ij +# +# # Probability of candidate c1 win elections +# totProb.ji <- P2.j + (1-SumP2) * P2ndround_c2 # P1.ij * P3.ij +# + PC1Win2ndround <- integrate(gammasum,lower = 0, upper = lim_sup, i=c1, alpha=alph, betta=beth)$value + PC2Win2ndround <- integrate(gammasum,lower = 0, upper = lim_sup, i=c2, alpha=alph, betta=beth)$value + + } + + cat("\nStoring data...") + + #Store Probabilities + if (target_round==1) { + tb_alphas[idx,] <- alph + Prob_c1c2_pass_2ndRnd[idx] <- P.c1c2 + Prob_c1_pass_2ndRnd[idx] <- P2ndround_c1 + Prob_c2_pass_2ndRnd[idx] <- P2ndround_c2 + Prob_c1_Win_1stRnd[idx] <- P2.i + Prob_c2_Win_1stRnd[idx] <- P2.j + Prob_None_Wins_1stRnd[idx] <- (1 - SumP2) + Prob_c1_wins_Elections[idx] <- totProb.ij + Prob_c2_wins_Elections[idx] <- totProb.ji + } else { +# Prob_c1_Win_2ndRnd[idx] <- P2.i +# Prob_c2_Win_2ndRnd[idx] <- P2.j +# Prob_c1_wins_Elections[idx] <- totProb.ij +# Prob_c2_wins_Elections[idx] <- totProb.ji + ProbInteg_c1_wins_Elections[idx] <- PC1Win2ndround + ProbInteg_c2_wins_Elections[idx] <- PC2Win2ndround + } + + idx <- idx + 1 + + } + + if (target_round==1) { + db_iter_stats <- rbind(db_iter_stats, data.frame ( + scenario = ScenIdx, + c1 = rep(c1, length(v_dates)), + c2 = rep(c2, length(v_dates)), + date = v_dates, + alphas = tb_alphas, + Prob_c1c2_pass_2ndRnd = Prob_c1c2_pass_2ndRnd, + Prob_c1_pass_2ndRnd = Prob_c1_pass_2ndRnd, + Prob_c2_pass_2ndRnd = Prob_c2_pass_2ndRnd, + Prob_c1_Win_1stRnd = Prob_c1_Win_1stRnd, + Prob_c2_Win_1stRnd = Prob_c2_Win_1stRnd, + Prob_None_Wins_1stRnd = Prob_None_Wins_1stRnd, + Prob_c1_wins_Elections = Prob_c1_wins_Elections, + Prob_c2_wins_Elections = Prob_c2_wins_Elections)) + } else { + db_iter_stats <- rbind(db_iter_stats, data.frame ( + scenario = ScenIdx, + c1 = rep(c1, length(v_dates)), + c2 = rep(c2, length(v_dates)), + date = v_dates, + alphas = tb_alphas, +# Prob_c1_Win_2ndRnd = Prob_c1_Win_2ndRnd, +# Prob_c2_Win_2ndRnd = Prob_c2_Win_2ndRnd, +# Prob_c1_wins_Elections = Prob_c1_wins_Elections, +# Prob_c2_wins_Elections = Prob_c2_wins_Elections, + ProbInteg_c1_wins_Elections <- ProbInteg_c1_wins_Elections, + ProbInteg_c2_wins_Elections <- ProbInteg_c2_wins_Elections)) + } + +} + +``` + +Posterior simulation + +```{r} +# Probability of candidates c1=1 and c2=3 pass to 2nd round + +v_dates <- db_iter_wrk %>% group_by(date) %>% pull(date) # %>% unique() +y <- as.integer(unname(as.matrix(db_iter_wrk %>% filter(date <= tail(v_dates,n=1)))[,paramlbl])) +y <- matrix(y,ncol=length(paramlbl),byrow=FALSE) + +# Calculus of probability of each candidate by simulation of Posterior Dirichlet +N <- 1000 + +sim_dir <- array(rep(0, nrow(y)*N*length(paramlbl)), dim=c(nrow(y),N,length(paramlbl))) +theta_CI <- list( + upper = array(rep(0,nrow(y)*length(paramlbl)),dim=c(nrow(y),length(paramlbl))), + lower = array(rep(0,nrow(y)*length(paramlbl)),dim=c(nrow(y),length(paramlbl))) +) + +theta_hist <- array(rep(0,nrow(y)*length(paramlbl)),dim=c(nrow(y),length(paramlbl))) + +sim_dir[1,,] <- rdirichlet(N, alpha=(y[1,]+rep(1,length(paramlbl)))) +theta_hist[1,] <- colMeans(sim_dir[1,,]) +theta_CI$upper[1,] <- theta_hist[1,]+qnorm(.975)*(apply(sim_dir[1,,],2,sd)/sqrt(N)) +theta_CI$lower[1,] <- theta_hist[1,]-qnorm(.975)*(apply(sim_dir[1,,],2,sd)/sqrt(N)) + +for (i in 2:nrow(y)) { + sim_dir[i,,] <- rdirichlet(N, alpha=(y[i,]+colMeans(sim_dir[i-1,,])*sum(y[i-1,]))) + theta_hist[i,] <- colMeans(sim_dir[i,,]) + theta_CI$upper[i,] <- theta_hist[i,]+qnorm(.975)*(apply(sim_dir[i,,],2,sd)/sqrt(N)) + theta_CI$lower[i,] <- theta_hist[i,]-qnorm(.975)*(apply(sim_dir[i,,],2,sd)/sqrt(N)) +} + +matplot(theta_hist[,1:2], type = c("b"), pch=1, col = 1:4) #plot +legend("topleft", legend = paramlbl[1:4], col=1:4, pch=1) # optional legend + +df <- data.frame() +for (j in 1:3) { + df <- rbind(df, + data.frame( + date = v_dates, + cand = paramlbl[j], + sup = theta_CI$upper[,j], + mid = theta_hist[,j], + inf = theta_CI$lower[,j]) + ) +} +ggplot(df, aes(x=date)) + + geom_line(aes(y=mid, color = cand), size=.8)+ + geom_point(aes(y=mid, color = cand), size=3) + + xlab("Date")+ylab("Pct. Votes")+ + scale_color_manual(breaks = c("Lula", "Bolsonaro", "Y-Blank/Null"), + values=c("red", "blue", "grey"))+ + theme_bw() + +``` + +... using MCMC for simulations + +```{r} +# Calculus of probability of each candidate by simulation of Posterior Dirichlet +N <- 1000 + +sim_dir <- array(rep(0, nrow(y)*N*length(paramlbl)), dim=c(nrow(y),N,length(paramlbl))) +theta_hist <- array(rep(0,nrow(y)*length(paramlbl)),dim=c(nrow(y),length(paramlbl))) +theta_CI <- list( + upper = array(rep(0,nrow(y)*length(paramlbl)),dim=c(nrow(y),length(paramlbl))), + lower = array(rep(0,nrow(y)*length(paramlbl)),dim=c(nrow(y),length(paramlbl))) +) + +sim_dir[1,,] <- MCMCpack::MCmultinomdirichlet(y[1,], alpha0 = (y[1,]+rep(1,length(paramlbl))), mc=N) +theta_hist[1,] <- colMeans(sim_dir[1,,]) +theta_CI$upper[1,] <- theta_hist[1,]+qnorm(.975)*(apply(sim_dir[1,,],2,sd)/sqrt(N)) +theta_CI$lower[1,] <- theta_hist[1,]-qnorm(.975)*(apply(sim_dir[1,,],2,sd)/sqrt(N)) + + +for (i in 2:nrow(y)) { + sim_dir[i,,] <- MCMCpack::MCmultinomdirichlet(y[i,], alpha0 = (y[i,]+colMeans(sim_dir[i-1,,])*sum(y[i-1,])), mc=N) + theta_hist[i,] <- colMeans(sim_dir[i,,]) + theta_CI$upper[i,] <- theta_hist[i,]+qnorm(.975)*(apply(sim_dir[i,,],2,sd)/sqrt(N)) + theta_CI$lower[i,] <- theta_hist[i,]-qnorm(.975)*(apply(sim_dir[i,,],2,sd)/sqrt(N)) +} + +matplot(theta_hist[,1:2], type = c("b"), pch=1, col = 1:4) #plot +legend("topleft", legend = paramlbl[1:4], col=1:4, pch=1) # optional legend + +for (j in 1:2) { + df <- data.frame( + sup = theta_CI$upper[,j], + mid = theta_hist[,j], + inf = theta_CI$lower[,j] + ) + p <- ggplot(df, aes(x=v_dates)) + + geom_line(aes(y=mid),color="black")+ + geom_point(aes(y=mid),color="black") + + geom_errorbar(aes(ymin=inf, ymax=sup), width=.2)+ + xlab("Date")+ylab(paramlbl[j])+ + theme_bw() + print(p) +} + +df <- data.frame() +for (j in 1:3) { + df <- rbind(df, + data.frame( + date = v_dates, + cand = paramlbl[j], + sup = theta_CI$upper[,j], + mid = theta_hist[,j], + inf = theta_CI$lower[,j]) + ) +} +ggplot(df, aes(x=date)) + + geom_line(aes(y=mid, color = cand), size=.8)+ + geom_point(aes(y=mid, color = cand), size=3) + + xlab("Date")+ylab("Pct. Votes")+ + scale_color_manual(breaks = c("Lula", "Bolsonaro", "Y-Blank/Null"), + values=c("red", "blue", "grey"))+ + theme_bw() +``` + +... using Integration + +```{r} +alph <- (y+colMeans(sim_dir[i-1,,])*sum(y[i-1,])) +sim_dir[1,,] <- rdirichlet(N, alpha=(y[1,]+rep(1,length(paramlbl)))) +theta_hist[1,] <- colMeans(sim_dir[1,,]) +theta_CI$upper[1,] <- theta_hist[1,]+qnorm(.975)*(apply(sim_dir[1,,],2,sd)/sqrt(N)) +theta_CI$lower[1,] <- theta_hist[1,]-qnorm(.975)*(apply(sim_dir[1,,],2,sd)/sqrt(N)) + +for (i in 2:nrow(y)) { + sim_dir[i,,] <- rdirichlet(N, alpha=(y[i,]+colMeans(sim_dir[i-1,,])*sum(y[i-1,]))) + theta_hist[i,] <- colMeans(sim_dir[i,,]) + theta_CI$upper[i,] <- theta_hist[i,]+qnorm(.975)*(apply(sim_dir[i,,],2,sd)/sqrt(N)) + theta_CI$lower[i,] <- theta_hist[i,]-qnorm(.975)*(apply(sim_dir[i,,],2,sd)/sqrt(N)) +} + +``` + + +Print Joint Scenarios + +```{r} +db_iter_stats %>% + mutate(pair = paste0(paramlbl[c1],"-",paramlbl[c2])) %>% + select(pair, date, Prob_c1c2_pass_2ndRnd) %>% + ggplot(aes(x=date, y=Prob_c1c2_pass_2ndRnd))+ + geom_point(aes(colour=pair), size=2.8) + + geom_line(aes(colour=pair), size=0.8) + + ggtitle(paste0("Probability of 2nd. round with these candidates - ",target_inst))+ + theme_bw() + +``` + + +Print Detailed Scenarios + +```{r} +# Plot Scenario +pltScenario <- function (idx) { + + Q <- list() + + # Auxiliary db + db_plt_wrk <- db_iter_stats %>% + filter(scenario == idx) + + Q[[1]] <- db_plt_wrk %>% + ggplot(aes(x=date))+ + geom_line(aes(y=Prob_c1_pass_2ndRnd), size=0.8, colour="blue") + + geom_point(aes(y=Prob_c1_pass_2ndRnd),size=2.4, colour="blue", shape="diamond") + + ggtitle(paste0("Probability of candidate ", + paramlbl[db_plt_wrk$c1[1]]," pass to 2nd round"))+ + xlab(paste0("Date - Inst. ", target_inst)) + + scale_color_manual(breaks = c("Lula", "Bolsonaro", "Y-Blank/Null"), + values=c("red", "blue", "grey"))+ + theme_bw() + + Q[[2]] <- db_plt_wrk %>% + ggplot(aes(x=date))+ + geom_line(aes(y=Prob_c2_pass_2ndRnd), size=0.8, colour="red") + + geom_point(aes(y=Prob_c2_pass_2ndRnd),size=2.4, colour="red", shape="diamond") + + ggtitle(paste0("Probability of candidate ", + paramlbl[db_plt_wrk$c2[1]]," pass to 2nd round"))+ + xlab(paste0("Date - Inst. ", target_inst)) + + scale_color_manual(breaks = c("Lula", "Bolsonaro", "Y-Blank/Null"), + values=c("red", "blue", "grey"))+ + theme_bw() + + Q[[3]] <- db_plt_wrk %>% + ggplot(aes(x=date))+ + geom_line(aes(y=Prob_c1_wins_Elections), size=0.8, colour="blue") + + geom_line(aes(y=Prob_c2_wins_Elections), size=0.8, colour="red") + + geom_point(aes(y=Prob_c1_wins_Elections),size=2.4, colour="blue") + + geom_point(aes(y=Prob_c2_wins_Elections),size=2.4, colour="red") + + ggtitle(paste0("Probability of candidates ", + paramlbl[db_plt_wrk$c1[1]]," and ",paramlbl[db_plt_wrk$c2[1]]," win the elections"))+ + xlab(paste0("Date - Inst. ", target_inst)) + + scale_color_manual(breaks = c("Lula", "Bolsonaro", "Y-Blank/Null"), + values=c("red", "blue", "grey"))+ + theme_bw() + + Q[[4]] <- db_plt_wrk %>% + ggplot(aes(x=date))+ + geom_line(aes(y=Prob_c1_Win_1stRnd), size=0.8, colour="blue") + + geom_line(aes(y=Prob_c2_Win_1stRnd), size=0.8, colour="red") + + geom_point(aes(y=Prob_c1_Win_1stRnd),size=2.4, colour="blue") + + geom_point(aes(y=Prob_c2_Win_1stRnd),size=2.4, colour="red") + + ggtitle(paste0("Probability of candidates ", + paramlbl[db_plt_wrk$c1[1]]," and ",paramlbl[db_plt_wrk$c2[1]]," win in 1st. Round"))+ + xlab(paste0("Date - Inst. ", target_inst)) + + scale_color_manual(breaks = c("Lula", "Bolsonaro", "Y-Blank/Null"), + values=c("red", "blue", "grey"))+ + theme_bw() + + Q[[5]] <- db_plt_wrk %>% + ggplot(aes(x=date))+ + geom_line(aes(y=Prob_c1c2_pass_2ndRnd), size=0.8, colour="black") + + geom_point(aes(y=Prob_c1c2_pass_2ndRnd),size=2.4) + + ggtitle(paste0("Probability of candidates ", + paramlbl[db_plt_wrk$c1[1]]," and ",paramlbl[db_plt_wrk$c2[1]]," are the most voted in 1st. round"))+ + xlab(paste0("Date - Inst. ", target_inst)) + + scale_color_manual(breaks = c("Lula", "Bolsonaro", "Y-Blank/Null"), + values=c("red", "blue", "grey"))+ + theme_bw() + + Q[[6]] <- db_plt_wrk %>% + ggplot(aes(x=date))+ + geom_line(aes(y=Prob_None_Wins_1stRnd), size=0.8, colour="black") + + geom_point(aes(y=Prob_None_Wins_1stRnd),size=2.4) + + ggtitle(paste0("Probability of no candidate wins in 1st. round"))+ + xlab(paste0("Date - Inst. ", target_inst)) + + scale_color_manual(breaks = c("Lula", "Bolsonaro", "Y-Blank/Null"), + values=c("red", "blue", "grey"))+ + theme_bw() + + Q[[7]] <- db_plt_wrk %>% + ggplot(aes(x=date))+ + geom_line(aes(y=ProbInteg_c1_wins_Elections), size=0.8, colour="blue") + + geom_line(aes(y=ProbInteg_c2_wins_Elections), size=0.8, colour="red") + + geom_point(aes(y=ProbInteg_c1_wins_Elections),size=2.4, colour="blue") + + geom_point(aes(y=ProbInteg_c2_wins_Elections),size=2.4, colour="red") + + ggtitle(paste0("Probability of candidates ", + paramlbl[db_plt_wrk$c1[1]]," and ",paramlbl[db_plt_wrk$c2[1]]," win elections"))+ + xlab(paste0("Date - Inst. ", target_inst)) + + scale_color_manual(breaks = c("Lula", "Bolsonaro", "Y-Blank/Null"), + values=c("red", "blue", "grey"))+ + theme_bw() + + return(Q) +} + +``` + + +```{r} +db_iter_stats %>% + filter(scenario == 1) %>% + ggplot(aes(x=date))+ + geom_line(aes(y=ProbInteg_c1_wins_Elections), size=0.8, colour="blue") + + geom_line(aes(y=ProbInteg_c2_wins_Elections), size=0.8, colour="red") + + geom_point(aes(y=ProbInteg_c1_wins_Elections),size=2.4, colour="blue") + + geom_point(aes(y=ProbInteg_c2_wins_Elections),size=2.4, colour="red") + + ggtitle(paste0("Probability of candidates ", + paramlbl[db_iter_stats$c1[1]]," and ",paramlbl[db_iter_stats$c2[1]]," win elections"))+ + xlab(paste0("Date - Inst. ", target_inst)) + + scale_color_manual(breaks = c("Lula", "Bolsonaro", "Y-Blank/Null"), + values=c("red", "blue", "grey"))+ + # geom_text(hjust=0, vjust=0)+ + theme_bw() +``` + + +```{r, fig.height=5, fig.width=6.5} +# Q <- pltScenario(1) + +# Scenario 2018 +Q <- pltScenario(1) + +if (target_round==1) { + Q_list <- c(1:6) +} else { + Q_list <- c(7) +} + +for (i in Q_list) + print(Q[[i]]) +``` + +Stan Compatibility Errors (workaround) - RUN ONCE! + +```{r} +#https://discourse.mc-stan.org/t/rstan-error-in-compilecode-f-code-language-language-verbose-verbose/28007/8 +#remove.packages(c("StanHeaders", "rstan")) +#install.packages("StanHeaders", repos = c("https://mc-stan.org/r-packages/", getOption("repos"))) +#install.packages("rstan", repos = c("https://mc-stan.org/r-packages/", getOption("repos"))) +``` + + +Stan Example (logit) + +```{r} +#https://eleafeit.com/posts/2021-05-23-parameterization-of-multinomial-logit-models-in-stan/ +``` + + +```{r} +library(bayesplot) +library(gtools) # rdirchlet +#rstan_options(auto_write=TRUE) # writes a compiled Stan program to the disk to avoid recompiling +#options(mc.cores = parallel::detectCores()) # uses multiple cores for stan +``` + +```{r} +#posterior <- as.matrix(fit_mn) +#mcmc_areas(posterior, +# pars = c(paste0("theta[",c(1,3),"]")), +# prob = 0.8) +#ppc_dens_overlay(y = fit_mn$y, +# yrep = posterior_predict(fit_mn, draws = 50)) +``` + +```{r} +set.seed(9) +X <- 1000*rt(1000,3) +mean(X) +hist(X) +sd(X) +plot(density(X)) + +require(graphics) + +1 - pt(1:5, df = 3) +qt(.975, df = c(1:10,20,50,100,1000)) + +tt <- seq(0, 10, length.out = 21) +ncp <- seq(0, 6, length.out = 31) +ptn <- outer(tt, ncp, function(t, d) pt(t, df = 3, ncp = d)) +t.tit <- "Non-central t - Probabilities" +image(tt, ncp, ptn, zlim = c(0,1), main = t.tit) +persp(tt, ncp, ptn, zlim = 0:1, r = 2, phi = 20, theta = 200, main = t.tit, + xlab = "t", ylab = "non-centrality parameter", + zlab = "Pr(T <= t)") + +plot(function(x) dt(x, df = 3, ncp = 2), -3, 11, ylim = c(0, 0.32), + main = "Non-central t - Density", yaxs = "i") +``` + + +```{r} +set.seed(853) + +rep(1:5, times=3) + +newborn_weight <- + tibble( + weight = + rep( + x = rnorm( + n = 1000, + mean = 3.5, + sd = 0.5 + ), + times = 3 + ), + measurement = c( + rep("Actual", 1000), + rep("Censored", 1000), + rep("Truncated", 1000) + ) + ) + +newborn_weight <- + newborn_weight |> + mutate( + weight = if_else( + weight <= 2.75 & measurement == "Censored", + 2.75, + weight + ), + weight = if_else( + weight >= 4.25 & measurement == "Truncated", + NA_real_, + weight + ) + ) + +newborn_weight |> + ggplot(aes(x = weight)) + + geom_histogram(bins = 50) + + facet_wrap(vars(measurement)) + + theme_minimal() + +remove(t) + +``` + +```{r} +library(tidyverse) +library(jsonlite) + +url_pres_br = "https://resultados.tse.jus.br/oficial/ele2022/544/dados-simplificados/br/br-c0001-e000544-r.json" + +pres_br <- fromJSON(url_pres_br, simplifyDataFrame = TRUE) %>% + .[["cand"]] %>% + tbl_df() +``` + +8.4.2 Probabilistic Sampling (R. Alexander, Chap 8th) + +\subsection{Simple Random Sampling} + +```{r} +set.seed(853) + +illustrative_sampling <- + tibble( + unit = 1:100, + simple_random_sampling = + sample( + x = c("Included", "Not included"), + size = 100, + replace = TRUE, + prob = c(0.2, 0.8) + ) + ) + +illustrative_sampling |> + count(simple_random_sampling) +``` + +\subsection{Systematic Sampling} + +```{r} +set.seed(853) + +starting_point <- sample( + x = c(1:5), + size = 1 +) + +illustrative_sampling <- + illustrative_sampling |> + mutate( + systematic_sampling = + if_else( + unit %in% seq.int( + from = starting_point, + to = 100, + by = 5 + ), + "Included", + "Not included" + ) + ) + +illustrative_sampling |> + count(systematic_sampling) +``` + +\subsection{Stratified Sampling} + +```{r} +set.seed(853) + +selected_within_strata <- + illustrative_sampling |> + mutate(strata = (unit - 1) %/% 10) |> + group_by(strata) |> + slice_sample(n = 2) |> + pull(unit) + +illustrative_sampling <- + illustrative_sampling |> + mutate(stratified_sampling = if_else( + unit %in% selected_within_strata, + "Included", + "Not included" + )) + +illustrative_sampling |> + count(stratified_sampling) +``` + +\subsection{Cluster Sampling} + +```{r} +set.seed(853) + +selected_clusters <- + sample( + x = c(0:9), + size = 2 + ) + +illustrative_sampling <- + illustrative_sampling |> + mutate( + cluster = (unit - 1) %/% 10, + cluster_sampling = if_else( + cluster %in% selected_clusters, + "Included", + "Not included" + ) + ) |> + select(-cluster) + +illustrative_sampling |> + count(cluster_sampling) +``` + +\subsection{Comparative} + +```{r} +new_labels <- c( + simple_random_sampling = "Simple random sampling", + systematic_sampling = "Systematic sampling", + stratified_sampling = "Stratified sampling", + cluster_sampling = "Cluster sampling" +) + +illustrative_sampling_long <- + illustrative_sampling |> + pivot_longer( + cols = c( + simple_random_sampling, + systematic_sampling, + stratified_sampling, + cluster_sampling + ), + names_to = "sampling_method", + values_to = "in_sample" + ) |> + mutate(sampling_method = factor( + sampling_method, + levels = c( + "simple_random_sampling", + "systematic_sampling", + "stratified_sampling", + "cluster_sampling" + ) + )) + +illustrative_sampling_long |> + filter(in_sample == "Included") |> + ggplot(aes(x = unit, y = in_sample)) + + geom_point() + + facet_wrap( + vars(sampling_method), + dir = "v", + ncol = 1, + labeller = labeller(sampling_method = new_labels) + ) + + theme_minimal() + + labs( + x = "Unit", + y = "Is included in sample" + ) + + theme(axis.text.y = element_blank()) +``` + +```{r} +illustrative_sampling_long |> + filter(in_sample == "Included") |> + group_by(sampling_method) |> + summarize(sum_from_sample = sum(unit)) |> + mutate(scaled_by_five = sum_from_sample * 5) |> + knitr::kable( + col.names = c( + "Sampling method", + "Sum of sample", + "Implied population sum" + ), + format.args = list(big.mark = ",") + ) +``` + +\subsection{Estimation} + +```{r} +set.seed(853) + +sleep <- + tibble( + toddler_sleep = sample(x = c(2:14), size = 30, replace = TRUE), + difference = sample(x = c(0:3), size = 30, replace = TRUE), + parent_sleep = toddler_sleep - difference + ) |> + select(toddler_sleep, parent_sleep, difference) + +sleep +``` + +```{r} +sleep |> + summarize( + toddler_sleep_average = mean(toddler_sleep), + parent_sleep_average = mean(parent_sleep) + ) |> + knitr::kable( + col.names = c("Toddler sleep average", "Parent sleep average"), + format.args = list(big.mark = ","), + digits = 2 + ) +``` + +8.4.3 Non-Probability samples + diff --git a/.Rproj.user/B584AE7E/sources/prop/12DA31F2 b/.Rproj.user/B584AE7E/sources/prop/12DA31F2 new file mode 100644 index 0000000..e5e04bf --- /dev/null +++ b/.Rproj.user/B584AE7E/sources/prop/12DA31F2 @@ -0,0 +1,6 @@ +{ + "source_window_id": "", + "Source": "Source", + "cursorPosition": "662,35", + "scrollLine": "649" +} \ No newline at end of file diff --git a/.Rproj.user/B584AE7E/sources/prop/EA93BB00 b/.Rproj.user/B584AE7E/sources/prop/EA93BB00 index f2d04cd..c4a4cc9 100644 --- a/.Rproj.user/B584AE7E/sources/prop/EA93BB00 +++ b/.Rproj.user/B584AE7E/sources/prop/EA93BB00 @@ -5,7 +5,8 @@ "Source": "Source", "docOutlineVisible": "1", "rmdVisualCollapsedChunks": "", - "rmdVisualModeLocation": "359:954", - "cursorPosition": "448,0", - "scrollLine": "0" + "rmdVisualModeLocation": "14113:13929", + "cursorPosition": "734,0", + "scrollLine": "0", + "docOutlineSize": "118" } \ No newline at end of file diff --git a/.Rproj.user/B584AE7E/sources/prop/INDEX b/.Rproj.user/B584AE7E/sources/prop/INDEX index 4b8be34..e19ae3d 100644 --- a/.Rproj.user/B584AE7E/sources/prop/INDEX +++ b/.Rproj.user/B584AE7E/sources/prop/INDEX @@ -1,3 +1,4 @@ +C%3A%2FUsers%2Flacor%2FOneDrive%2F100.%20Personal%2FSelfDevelopment%2F20.%20Project%202019%2FBayesian-Inference%2FMetaPolls_vFINAL.Rmd="12DA31F2" C%3A%2FUsers%2Flacor%2FOneDrive%2F100.%20Personal%2FSelfDevelopment%2F20.%20Project%202019%2FBayesian-Inference%2FSamplinginPractice_v7.Rmd="706557E6" C%3A%2FUsers%2Flacor%2FOneDrive%2F100.%20Personal%2FSelfDevelopment%2F20.%20Project%202019%2FStockMarket%2FAsset_Analysis_v23.Rmd="8C7CB8DC" C%3A%2FUsers%2Flacor%2FOneDrive%2F100.%20Personal%2FSelfDevelopment%2F25.%20Project%202022%2FStatistical-Thinking-2023%2FStatistical%20Thinking%202023_v2.qmd="EA93BB00" diff --git a/.Rproj.user/B584AE7E/viewer-cache/1A4706DF.Rdata b/.Rproj.user/B584AE7E/viewer-cache/1A4706DF.Rdata new file mode 100644 index 0000000..e11675e Binary files /dev/null and b/.Rproj.user/B584AE7E/viewer-cache/1A4706DF.Rdata differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/B584AE7E26a0c196/chunks.json b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/B584AE7E26a0c196/chunks.json new file mode 100644 index 0000000..1e18157 --- /dev/null +++ b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/B584AE7E26a0c196/chunks.json @@ -0,0 +1 @@ +{"chunk_definitions":[{"row":15,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-1"},"document_id":"77F68D30","chunk_id":"c5ns7cfqmp4sr","chunk_label":"unnamed-chunk-1"},{"row":57,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-40"},"document_id":"77F68D30","chunk_id":"c0jrftmj331il","chunk_label":"unnamed-chunk-2"},{"row":63,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-41"},"document_id":"77F68D30","chunk_id":"cnq8c446n0wtz","chunk_label":"unnamed-chunk-3"},{"row":77,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-42"},"document_id":"77F68D30","chunk_id":"c3qooae84c4xs","chunk_label":"unnamed-chunk-4"},{"row":97,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-43"},"document_id":"77F68D30","chunk_id":"c8k36ylh3ik2z","chunk_label":"unnamed-chunk-5"},{"row":103,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-44"},"document_id":"77F68D30","chunk_id":"cff5zae4b4ix2","chunk_label":"unnamed-chunk-6"},{"row":117,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-45"},"document_id":"77F68D30","chunk_id":"cuj4f1k7oy0f9","chunk_label":"unnamed-chunk-7"},{"row":123,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-25"},"document_id":"77F68D30","chunk_id":"c3b4hkzi6i1gq","chunk_label":"unnamed-chunk-8"},{"row":139,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-47"},"document_id":"77F68D30","chunk_id":"cryc9nwjktom8","chunk_label":"unnamed-chunk-9"},{"row":144,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-48"},"document_id":"77F68D30","chunk_id":"c11kjcel8rada","chunk_label":"unnamed-chunk-10"},{"row":154,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"cu5f4f0k460vm","chunk_label":"unnamed-chunk-11"},{"row":161,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-50"},"document_id":"77F68D30","chunk_id":"cr9sbhfkl0l7m","chunk_label":"unnamed-chunk-12"},{"row":165,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-51"},"document_id":"77F68D30","chunk_id":"c67fek8fh1vdj","chunk_label":"unnamed-chunk-13"},{"row":169,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"c0qzt99s3uz17","chunk_label":"unnamed-chunk-14"},{"row":183,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-53"},"document_id":"77F68D30","chunk_id":"cf9t8qccs9es8","chunk_label":"unnamed-chunk-15"}],"doc_write_time":1674958149} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/B584AE7E2be10273/chunks.json b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/B584AE7E2be10273/chunks.json new file mode 100644 index 0000000..48c406f --- /dev/null +++ b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/B584AE7E2be10273/chunks.json @@ -0,0 +1 @@ +{"chunk_definitions":[{"row":15,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-24"},"document_id":"77F68D30","chunk_id":"c5ns7cfqmp4sr","chunk_label":"unnamed-chunk-1"},{"row":57,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-40"},"document_id":"77F68D30","chunk_id":"c0jrftmj331il","chunk_label":"unnamed-chunk-2"},{"row":63,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-41"},"document_id":"77F68D30","chunk_id":"cnq8c446n0wtz","chunk_label":"unnamed-chunk-3"},{"row":77,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-42"},"document_id":"77F68D30","chunk_id":"c3qooae84c4xs","chunk_label":"unnamed-chunk-4"},{"row":97,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-43"},"document_id":"77F68D30","chunk_id":"c8k36ylh3ik2z","chunk_label":"unnamed-chunk-5"},{"row":103,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-44"},"document_id":"77F68D30","chunk_id":"cff5zae4b4ix2","chunk_label":"unnamed-chunk-6"},{"row":362,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-20"},"document_id":"77F68D30","chunk_id":"c1tfzdz2b80hy","chunk_label":"unnamed-chunk-27"},{"row":435,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-37"},"document_id":"77F68D30","chunk_id":"c69x3euxyb1pe","chunk_label":"unnamed-chunk-30"},{"row":809,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-89"},"document_id":"77F68D30","chunk_id":"crdz8pq0jk07z","chunk_label":"unnamed-chunk-52"},{"row":687,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"csirmd4u6rxjw","chunk_label":"unnamed-chunk-50"},{"row":683,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-51"},"document_id":"77F68D30","chunk_id":"c7jwe67ih7e5t","chunk_label":"unnamed-chunk-49"},{"row":584,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-35"},"document_id":"77F68D30","chunk_id":"csmeeql20tusx","chunk_label":"unnamed-chunk-42"},{"row":663,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-83"},"document_id":"77F68D30","chunk_id":"cuugb0843jsqr","chunk_label":"unnamed-chunk-47"},{"row":650,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","message":false,"label":"unnamed-chunk-94"},"document_id":"77F68D30","chunk_id":"cgq5kq1n6i5pg","chunk_label":"unnamed-chunk-45"},{"row":117,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-45"},"document_id":"77F68D30","chunk_id":"cuj4f1k7oy0f9","chunk_label":"unnamed-chunk-7"},{"row":123,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-25"},"document_id":"77F68D30","chunk_id":"c3b4hkzi6i1gq","chunk_label":"unnamed-chunk-8"},{"row":575,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-34"},"document_id":"77F68D30","chunk_id":"cwztp6gqf9w0s","chunk_label":"unnamed-chunk-41"},{"row":565,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-32"},"document_id":"77F68D30","chunk_id":"cozvs9s6hxsfi","chunk_label":"unnamed-chunk-40"},{"row":139,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-47"},"document_id":"77F68D30","chunk_id":"cryc9nwjktom8","chunk_label":"unnamed-chunk-9"},{"row":144,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-48"},"document_id":"77F68D30","chunk_id":"c11kjcel8rada","chunk_label":"unnamed-chunk-10"},{"row":154,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"cu5f4f0k460vm","chunk_label":"unnamed-chunk-11"},{"row":161,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-50"},"document_id":"77F68D30","chunk_id":"cr9sbhfkl0l7m","chunk_label":"unnamed-chunk-12"},{"row":165,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-51"},"document_id":"77F68D30","chunk_id":"c67fek8fh1vdj","chunk_label":"unnamed-chunk-13"},{"row":169,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"c0qzt99s3uz17","chunk_label":"unnamed-chunk-14"},{"row":183,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"c0qzt99s3uz17","chunk_label":"unnamed-chunk-15"},{"row":191,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"c0qzt99s3uz17","chunk_label":"unnamed-chunk-16"},{"row":198,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"c0qzt99s3uz17","chunk_label":"unnamed-chunk-17"},{"row":559,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-8"},"document_id":"77F68D30","chunk_id":"cct9s4x1m0a0t","chunk_label":"unnamed-chunk-39"},{"row":539,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-7"},"document_id":"77F68D30","chunk_id":"cowzykbnv5xxz","chunk_label":"unnamed-chunk-38"},{"row":521,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-6"},"document_id":"77F68D30","chunk_id":"czw0rd1u0m33e","chunk_label":"unnamed-chunk-37"},{"row":505,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-49"},"document_id":"77F68D30","chunk_id":"cp6949hwjcirq","chunk_label":"unnamed-chunk-36"},{"row":492,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-49"},"document_id":"77F68D30","chunk_id":"cp6949hwjcirq","chunk_label":"unnamed-chunk-35"},{"row":482,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-49"},"document_id":"77F68D30","chunk_id":"cp6949hwjcirq","chunk_label":"unnamed-chunk-34"},{"row":465,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-49"},"document_id":"77F68D30","chunk_id":"cp6949hwjcirq","chunk_label":"unnamed-chunk-33"},{"row":455,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","fig.height":3.5,"fig.width":6.5,"label":"unnamed-chunk-65"},"document_id":"77F68D30","chunk_id":"cdj7r7sin50xc","chunk_label":"unnamed-chunk-32"},{"row":447,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","fig.height":3.5,"fig.width":6.5,"label":"unnamed-chunk-65"},"document_id":"77F68D30","chunk_id":"cdj7r7sin50xc","chunk_label":"unnamed-chunk-31"},{"row":399,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-2"},"document_id":"77F68D30","chunk_id":"cwqxet0zexx43","chunk_label":"unnamed-chunk-29"},{"row":369,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-59"},"document_id":"77F68D30","chunk_id":"c85mkxl3u2xgh","chunk_label":"unnamed-chunk-28"},{"row":353,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-59"},"document_id":"77F68D30","chunk_id":"c85mkxl3u2xgh","chunk_label":"unnamed-chunk-26"},{"row":337,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-59"},"document_id":"77F68D30","chunk_id":"c85mkxl3u2xgh","chunk_label":"unnamed-chunk-25"},{"row":314,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-59"},"document_id":"77F68D30","chunk_id":"c85mkxl3u2xgh","chunk_label":"unnamed-chunk-24"},{"row":305,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-59"},"document_id":"77F68D30","chunk_id":"c85mkxl3u2xgh","chunk_label":"unnamed-chunk-23"},{"row":285,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-59"},"document_id":"77F68D30","chunk_id":"c85mkxl3u2xgh","chunk_label":"unnamed-chunk-22"},{"row":244,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-59"},"document_id":"77F68D30","chunk_id":"c85mkxl3u2xgh","chunk_label":"unnamed-chunk-21"},{"row":238,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-59"},"document_id":"77F68D30","chunk_id":"c85mkxl3u2xgh","chunk_label":"unnamed-chunk-20"},{"row":211,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"c0qzt99s3uz17","chunk_label":"unnamed-chunk-19"},{"row":203,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"c0qzt99s3uz17","chunk_label":"unnamed-chunk-18"},{"row":824,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-92"},"document_id":"77F68D30","chunk_id":"cxaye4qsd68kd","chunk_label":"unnamed-chunk-55"},{"row":819,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-91"},"document_id":"77F68D30","chunk_id":"csibvqbb2q9r9","chunk_label":"unnamed-chunk-54"},{"row":669,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-50"},"document_id":"77F68D30","chunk_id":"czdkbdsep34zc","chunk_label":"unnamed-chunk-48"},{"row":815,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-90"},"document_id":"77F68D30","chunk_id":"cvya5ncyop03f","chunk_label":"unnamed-chunk-53"}],"doc_write_time":1674959533,"chunk_rendered_width":661} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/B584AE7Ee269a276/c5ns7cfqmp4sr/00000f.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/B584AE7Ee269a276/c5ns7cfqmp4sr/00000f.csv new file mode 100644 index 0000000..74171d9 --- /dev/null +++ b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/B584AE7Ee269a276/c5ns7cfqmp4sr/00000f.csv @@ -0,0 +1,49 @@ +"0","#library(rethinking)" +"0","library(tidyverse)" +"2","Registered S3 methods overwritten by 'dbplyr': + method from + print.tbl_lazy + print.tbl_sql +" +"2","── Attaching packages ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.2 ──" +"2","✔ ggplot2 3.4.0 ✔ purrr  0.3.5 +✔ tibble  3.1.8 ✔ dplyr  1.0.10 +✔ tidyr  1.2.1 ✔ stringr 1.4.1 +✔ readr  2.1.3 ✔ forcats 0.5.2 " +"2","Warning: package ‘ggplot2’ was built under R version 4.2.2" +"2","Warning: package ‘tidyr’ was built under R version 4.2.2" +"2","Warning: package ‘readr’ was built under R version 4.2.2" +"2","Warning: package ‘purrr’ was built under R version 4.2.2" +"2","Warning: package ‘dplyr’ was built under R version 4.2.2" +"2","── Conflicts ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ── +✖ dplyr::filter() masks stats::filter() +✖ dplyr::lag() masks stats::lag()" +"0","library(dplyr)" +"0","library(ggplot2)" +"0","library(rstan)" +"2","Loading required package: StanHeaders +" +"2"," +rstan version 2.26.13 (Stan version 2.26.1) + +" +"2","For execution on a local, multicore CPU with excess RAM we recommend calling +options(mc.cores = parallel::detectCores()). +To avoid recompilation of unchanged Stan programs, we recommend calling +rstan_options(auto_write = TRUE) +For within-chain threading using `reduce_sum()` or `map_rect()` Stan functions, +change `threads_per_chain` option: +rstan_options(threads_per_chain = 1) + +" +"2","Do not specify '-march=native' in 'LOCAL_CPPFLAGS' or a Makevars file +" +"2"," +Attaching package: ‘rstan’ + +" +"2","The following object is masked from ‘package:tidyr’: + + extract + +" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/B584AE7Ee269a276/chunks.json b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/B584AE7Ee269a276/chunks.json new file mode 100644 index 0000000..1bbe887 --- /dev/null +++ b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/B584AE7Ee269a276/chunks.json @@ -0,0 +1 @@ +{"chunk_definitions":[{"row":15,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-1"},"document_id":"77F68D30","chunk_id":"c5ns7cfqmp4sr","chunk_label":"unnamed-chunk-1"},{"row":57,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-40"},"document_id":"77F68D30","chunk_id":"c0jrftmj331il","chunk_label":"unnamed-chunk-2"},{"row":63,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-41"},"document_id":"77F68D30","chunk_id":"cnq8c446n0wtz","chunk_label":"unnamed-chunk-3"},{"row":77,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-42"},"document_id":"77F68D30","chunk_id":"c3qooae84c4xs","chunk_label":"unnamed-chunk-4"},{"row":97,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-43"},"document_id":"77F68D30","chunk_id":"c8k36ylh3ik2z","chunk_label":"unnamed-chunk-5"},{"row":103,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-44"},"document_id":"77F68D30","chunk_id":"cff5zae4b4ix2","chunk_label":"unnamed-chunk-6"},{"row":117,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-45"},"document_id":"77F68D30","chunk_id":"cuj4f1k7oy0f9","chunk_label":"unnamed-chunk-7"},{"row":123,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-25"},"document_id":"77F68D30","chunk_id":"c3b4hkzi6i1gq","chunk_label":"unnamed-chunk-8"},{"row":139,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-47"},"document_id":"77F68D30","chunk_id":"cryc9nwjktom8","chunk_label":"unnamed-chunk-9"},{"row":144,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-48"},"document_id":"77F68D30","chunk_id":"c11kjcel8rada","chunk_label":"unnamed-chunk-10"},{"row":154,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"cu5f4f0k460vm","chunk_label":"unnamed-chunk-11"},{"row":213,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-56"},"document_id":"77F68D30","chunk_id":"cuafhnftbidp9","chunk_label":""},{"row":213,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-56"},"document_id":"77F68D30","chunk_id":"cuafhnftbidp9","chunk_label":""},{"row":213,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-56"},"document_id":"77F68D30","chunk_id":"cuafhnftbidp9","chunk_label":""},{"row":213,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-56"},"document_id":"77F68D30","chunk_id":"cuafhnftbidp9","chunk_label":""},{"row":213,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-56"},"document_id":"77F68D30","chunk_id":"cuafhnftbidp9","chunk_label":""},{"row":213,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-56"},"document_id":"77F68D30","chunk_id":"cuafhnftbidp9","chunk_label":""},{"row":213,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-56"},"document_id":"77F68D30","chunk_id":"cuafhnftbidp9","chunk_label":""},{"row":213,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-56"},"document_id":"77F68D30","chunk_id":"cuafhnftbidp9","chunk_label":""}],"doc_write_time":1674958149} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c211ibf7ouri8/000036.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c211ibf7ouri8/000036.csv deleted file mode 100644 index b2a4d72..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c211ibf7ouri8/000036.csv +++ /dev/null @@ -1,4 +0,0 @@ -"0","p <- seq(from=.01, to=1, by=0.01); ptr <- .95" -"0","true_pos <- sapply(p, function (q) (ptr*q)/(ptr*q+(1-ptr)*(1-q)))" -"0","" -"0","plot(true_pos, p, col=2, lwd=3)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c211ibf7ouri8/000037.png b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c211ibf7ouri8/000037.png deleted file mode 100644 index 156d475..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c211ibf7ouri8/000037.png and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c211ibf7ouri8/000037.snapshot b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c211ibf7ouri8/000037.snapshot deleted file mode 100644 index 769f0ec..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c211ibf7ouri8/000037.snapshot and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c2fn3g4qdmzyy/00000f.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c2fn3g4qdmzyy/00000f.csv deleted file mode 100644 index cf31ab0..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c2fn3g4qdmzyy/00000f.csv +++ /dev/null @@ -1,7 +0,0 @@ -"0","library(rethinking)" -"0","" -"0","sim_weight <- function (H, b, sd) {" -"0"," U <- rnorm(length(H),0,sd)" -"0"," W <- b*H + U" -"0"," return(W)" -"0","}" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c2jly3c97ig8i/00000f.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c2jly3c97ig8i/00000f.csv deleted file mode 100644 index 918d6cb..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c2jly3c97ig8i/00000f.csv +++ /dev/null @@ -1,4 +0,0 @@ -"0","library(rethinking)" -"0","" -"0","data(Howell1)" -"0","d <-Howell1[Howell1$age>=18,]" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c4d2y4qjjl0e3/000011.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c4d2y4qjjl0e3/000011.csv deleted file mode 100644 index 35cb05b..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c4d2y4qjjl0e3/000011.csv +++ /dev/null @@ -1,7 +0,0 @@ -"0","# S = 1: female, S=2: male" -"0","sim_HW <- function(S, b, a) {" -"0"," N <- length(S)" -"0"," H <- ifelse(S==1,150,160)+rnorm(N,0,5)" -"0"," W <- a[S] + b[S]*H + rnorm(N, 0, 5)" -"0"," data.frame(S,H,W)" -"0","}" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003b.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003b.csv deleted file mode 100644 index d8394af..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003b.csv +++ /dev/null @@ -1,4 +0,0 @@ -"0","d |> " -"0"," ggplot(aes(x=height, y=weight, color=factor(male)))+" -"0"," geom_point()+" -"0"," theme_bw()" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003c.png b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003c.png deleted file mode 100644 index 33f987e..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003c.png and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003c.snapshot b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003c.snapshot deleted file mode 100644 index 970b031..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003c.snapshot and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003d.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003d.csv deleted file mode 100644 index 7df6cb7..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003d.csv +++ /dev/null @@ -1,5 +0,0 @@ -"0","" -"0","d |> " -"0"," ggplot(aes(x=weight))+" -"0"," geom_density(aes(color=factor(male)))+" -"0"," theme_bw()" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003e.png b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003e.png deleted file mode 100644 index f6ea349..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003e.png and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003e.snapshot b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003e.snapshot deleted file mode 100644 index 85be33f..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003e.snapshot and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003f.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003f.csv deleted file mode 100644 index 6beaa18..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003f.csv +++ /dev/null @@ -1,5 +0,0 @@ -"0","" -"0","d |> " -"0"," ggplot(aes(x=height))+" -"0"," geom_density(aes(color=factor(male)))+" -"0"," theme_bw()" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/000040.metadata b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/000040.metadata deleted file mode 100644 index 6399179..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/000040.metadata +++ /dev/null @@ -1 +0,0 @@ -{"height":408.5290482076638,"width":661.0,"size_behavior":0,"conditions":[]} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/000040.png b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/000040.png deleted file mode 100644 index 7048395..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/000040.png and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/000040.snapshot b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/000040.snapshot deleted file mode 100644 index 1cf6bd3..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/000040.snapshot and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c5ns7cfqmp4sr/00000f.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c5ns7cfqmp4sr/00000f.csv index fc65085..6ac69ea 100644 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c5ns7cfqmp4sr/00000f.csv +++ b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c5ns7cfqmp4sr/00000f.csv @@ -1,23 +1,4 @@ "0","library(rethinking)" -"2","Loading required package: rstan -" -"2","Loading required package: StanHeaders -" -"2"," -rstan version 2.26.13 (Stan version 2.26.1) - -" -"2","For execution on a local, multicore CPU with excess RAM we recommend calling -options(mc.cores = parallel::detectCores()). -To avoid recompilation of unchanged Stan programs, we recommend calling -rstan_options(auto_write = TRUE) -For within-chain threading using `reduce_sum()` or `map_rect()` Stan functions, -change `threads_per_chain` option: -rstan_options(threads_per_chain = 1) - -" -"2","Do not specify '-march=native' in 'LOCAL_CPPFLAGS' or a Makevars file -" "2","Loading required package: cmdstanr " "2","This is cmdstanr version 0.5.3 @@ -40,6 +21,11 @@ Attaching package: ‘rethinking’ stan +" +"2","The following object is masked from ‘package:purrr’: + + map + " "2","The following object is masked from ‘package:stats’: @@ -47,25 +33,6 @@ Attaching package: ‘rethinking’ " "0","library(tidyverse)" -"2","Registered S3 methods overwritten by 'dbplyr': - method from - print.tbl_lazy - print.tbl_sql -" -"2","── Attaching packages ───────────────────────────────────────────────────── tidyverse 1.3.2 ──" -"2","✔ ggplot2 3.4.0 ✔ purrr  0.3.5 -✔ tibble  3.1.8 ✔ dplyr  1.0.10 -✔ tidyr  1.2.1 ✔ stringr 1.4.1 -✔ readr  2.1.3 ✔ forcats 0.5.2 " -"2","Warning: package ‘ggplot2’ was built under R version 4.2.2" -"2","Warning: package ‘tidyr’ was built under R version 4.2.2" -"2","Warning: package ‘readr’ was built under R version 4.2.2" -"2","Warning: package ‘purrr’ was built under R version 4.2.2" -"2","Warning: package ‘dplyr’ was built under R version 4.2.2" -"2","── Conflicts ──────────────────────────────────────────────────────── tidyverse_conflicts() ── -✖ tidyr::extract() masks rstan::extract() -✖ dplyr::filter() masks stats::filter() -✖ dplyr::lag() masks stats::lag() -✖ purrr::map() masks rethinking::map()" "0","library(dplyr)" "0","library(ggplot2)" +"0","library(rstan)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001b.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001b.csv deleted file mode 100644 index 5d79829..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001b.csv +++ /dev/null @@ -1,2 +0,0 @@ -"0","post <- extract.samples(m3.2)" -"0","pairs(post)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001c.metadata b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001c.metadata deleted file mode 100644 index 6399179..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001c.metadata +++ /dev/null @@ -1 +0,0 @@ -{"height":408.5290482076638,"width":661.0,"size_behavior":0,"conditions":[]} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001c.png b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001c.png deleted file mode 100644 index f85a7d7..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001c.png and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001c.snapshot b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001c.snapshot deleted file mode 100644 index 55011a8..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001c.snapshot and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001d.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001d.csv deleted file mode 100644 index 7be7e9a..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001d.csv +++ /dev/null @@ -1 +0,0 @@ -"0","head(post)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001e.rdf b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001e.rdf deleted file mode 100644 index 2b98f10..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001e.rdf and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001f.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001f.csv deleted file mode 100644 index 64d6e07..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001f.csv +++ /dev/null @@ -1,2 +0,0 @@ -"0","plot(d$height,d$weight,col=2,lwd=3," -"0"," xlab=""height(cm)"", ylab=""weight(kg)"")" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/000021.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/000021.csv deleted file mode 100644 index 5506a66..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/000021.csv +++ /dev/null @@ -1,2 +0,0 @@ -"0","for (j in 1:20)" -"0"," abline(a=post$a[j],b=post$b[j], lty=1)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/000023.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/000023.csv deleted file mode 100644 index 2cf263d..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/000023.csv +++ /dev/null @@ -1,5 +0,0 @@ -"0","" -"0","height_seq <- seq(130,190,len=20)" -"0","W_postpred <- sim(m3.2,data=list(H=height_seq))" -"0","W_PI <- apply(W_postpred,2,PI)" -"0","lines(height_seq,W_PI[1,],lty=2,lwd=2)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/000025.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/000025.csv deleted file mode 100644 index 2f93955..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/000025.csv +++ /dev/null @@ -1 +0,0 @@ -"0","lines(height_seq,W_PI[2,],lty=2,lwd=2)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/000026.metadata b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/000026.metadata deleted file mode 100644 index 6399179..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/000026.metadata +++ /dev/null @@ -1 +0,0 @@ -{"height":408.5290482076638,"width":661.0,"size_behavior":0,"conditions":[]} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/000026.png b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/000026.png deleted file mode 100644 index eef685a..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/000026.png and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/000026.snapshot b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/000026.snapshot deleted file mode 100644 index d414226..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/000026.snapshot and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c7jwe67ih7e5t/000011.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c7jwe67ih7e5t/000011.csv new file mode 100644 index 0000000..df1b7d0 --- /dev/null +++ b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c7jwe67ih7e5t/000011.csv @@ -0,0 +1,11 @@ +"0","d$weight_s <-(d$weight-mean(d$weight))/sd(d$weight)" +"0","d$weight_s2 <-d$weight_s^2" +"0","m4.5 <-quap(" +"0"," alist(" +"0"," height ~dnorm(mu,sigma)," +"0"," mu <-a+b1*weight_s+b2*weight_s2," +"0"," a ~dnorm(178,20)," +"0"," b1 ~dlnorm(0,1)," +"0"," b2 ~dnorm(0,1)," +"0"," sigma ~dunif(0,50)" +"0"," ) ,data=d)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c8e0bbjupr09b/000019.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c8e0bbjupr09b/000019.csv deleted file mode 100644 index f4a1c9c..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c8e0bbjupr09b/000019.csv +++ /dev/null @@ -1,29 +0,0 @@ -"0","i <- 5" -"0","L <- SSampl[SSlice[i]]" -"0","# quantile(L[[1]],probs=c(.025,.975))" -"0","percs[SSlice][i-1]" -"1","[[1]] -" -"1"," 2.5% " -"1"," 97.5% " -"1"," -" -"1","-6.485166 " -"1","10.894424 " -"1"," -" -"1"," -" -"0","percs[SSlice][i]" -"1","[[1]] -" -"1"," 2.5% " -"1"," 97.5% " -"1"," -" -"1","-8.811031 " -"1"," 8.672646 " -"1"," -" -"1"," -" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c9ajju6cf9cv8/000011.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c9ajju6cf9cv8/000011.csv deleted file mode 100644 index 0514915..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c9ajju6cf9cv8/000011.csv +++ /dev/null @@ -1,16 +0,0 @@ -"0","the_sample <- sim_globe()" -"0","" -"0","print(the_sample)" -"1","[1]" -"1"," ""W""" -"1"," ""L""" -"1"," ""W""" -"1"," ""L""" -"1"," ""W""" -"1"," ""W""" -"1"," ""L""" -"1"," ""W""" -"1"," ""L""" -"1"," -" -"0","compute_posterior(the_sample)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c9ajju6cf9cv8/000012.metadata b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c9ajju6cf9cv8/000012.metadata deleted file mode 100644 index 6399179..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c9ajju6cf9cv8/000012.metadata +++ /dev/null @@ -1 +0,0 @@ -{"height":408.5290482076638,"width":661.0,"size_behavior":0,"conditions":[]} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c9ajju6cf9cv8/000012.png b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c9ajju6cf9cv8/000012.png deleted file mode 100644 index ebdef74..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c9ajju6cf9cv8/000012.png and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c9ajju6cf9cv8/000012.snapshot b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c9ajju6cf9cv8/000012.snapshot deleted file mode 100644 index b6737d4..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c9ajju6cf9cv8/000012.snapshot and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cct9s4x1m0a0t/00000f.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cct9s4x1m0a0t/000011.csv similarity index 100% rename from .Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cct9s4x1m0a0t/00000f.csv rename to .Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cct9s4x1m0a0t/000011.csv diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cefh5ihzkcqbl/000012.metadata b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cct9s4x1m0a0t/000012.metadata similarity index 100% rename from .Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cefh5ihzkcqbl/000012.metadata rename to .Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cct9s4x1m0a0t/000012.metadata diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cct9s4x1m0a0t/000010.rdf b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cct9s4x1m0a0t/000012.rdf similarity index 55% rename from .Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cct9s4x1m0a0t/000010.rdf rename to .Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cct9s4x1m0a0t/000012.rdf index 817d1d5..1b01e46 100644 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cct9s4x1m0a0t/000010.rdf and b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cct9s4x1m0a0t/000012.rdf differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cefh5ihzkcqbl/000011.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cefh5ihzkcqbl/000011.csv deleted file mode 100644 index 6a78395..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cefh5ihzkcqbl/000011.csv +++ /dev/null @@ -1,17 +0,0 @@ -"0","#Similate a sample of 10 people" -"0","set.seed(93)" -"0","H <- runif(10,130,170)" -"0","W <- sim_weight(H,b=0.5, sd=5)" -"0","" -"0","#Run Model" -"0","m3.1 <- quap(" -"0"," alist(" -"0"," W ~ dnorm(mu,sigma)," -"0"," mu <- a + b*H," -"0"," a ~ dnorm(0,10)," -"0"," b ~ dunif(0,1)," -"0"," sigma~dunif(0,10)" -"0","),data=list(W=W, H=H))" -"0","" -"0","#Summary" -"0","precis(m3.1)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cefh5ihzkcqbl/000012.rdf b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cefh5ihzkcqbl/000012.rdf deleted file mode 100644 index 806c23f..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cefh5ihzkcqbl/000012.rdf and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cf9t8qccs9es8/00000f.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cf9t8qccs9es8/00000f.csv deleted file mode 100644 index de55960..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cf9t8qccs9es8/00000f.csv +++ /dev/null @@ -1,13 +0,0 @@ -"0","library(ggplot2)" -"0","" -"0","compute_posterior <- function (the_sample, poss = c(0,.25,.5,.75,1)) {" -"0"," W <- sum(the_sample=='W') # No. of 'W'" -"0"," L <- sum(the_sample=='L') # No. of 'L'" -"0"," ways <- sapply(p, function (q) (q*4)^W*((1-q)*4)^L)" -"0"," post <- ways/sum(ways)" -"0"," # bars <- sapply(post, function (q) make_bar(q))" -"0"," # data.frame(poss, ways, post=round(post,3), bars)" -"0"," df <- data.frame(poss, ways, post=round(post,3))" -"0"," ggplot(aes(x=poss, y=post), data=df)+" -"0"," geom_bar(stat=""identity"")" -"0","}" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cfk35uawz2hsn/000011.metadata b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cfk35uawz2hsn/000011.metadata deleted file mode 100644 index 6399179..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cfk35uawz2hsn/000011.metadata +++ /dev/null @@ -1 +0,0 @@ -{"height":408.5290482076638,"width":661.0,"size_behavior":0,"conditions":[]} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cfk35uawz2hsn/000011.png b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cfk35uawz2hsn/000011.png deleted file mode 100644 index b722523..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cfk35uawz2hsn/000011.png and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cfk35uawz2hsn/000011.snapshot b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cfk35uawz2hsn/000011.snapshot deleted file mode 100644 index d5ef024..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cfk35uawz2hsn/000011.snapshot and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/chunks.json b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/chunks.json index 70993ea..48c406f 100644 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/chunks.json +++ b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/chunks.json @@ -1 +1 @@ -{"chunk_definitions":[{"row":56,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-40"},"document_id":"77F68D30","chunk_id":"c0jrftmj331il","chunk_label":"unnamed-chunk-2"},{"row":62,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-41"},"document_id":"77F68D30","chunk_id":"cnq8c446n0wtz","chunk_label":"unnamed-chunk-3"},{"row":76,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-42"},"document_id":"77F68D30","chunk_id":"c3qooae84c4xs","chunk_label":"unnamed-chunk-4"},{"row":96,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-43"},"document_id":"77F68D30","chunk_id":"c8k36ylh3ik2z","chunk_label":"unnamed-chunk-5"},{"row":102,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-44"},"document_id":"77F68D30","chunk_id":"cff5zae4b4ix2","chunk_label":"unnamed-chunk-6"},{"row":116,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-45"},"document_id":"77F68D30","chunk_id":"cuj4f1k7oy0f9","chunk_label":"unnamed-chunk-7"},{"row":122,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-25"},"document_id":"77F68D30","chunk_id":"c3b4hkzi6i1gq","chunk_label":"unnamed-chunk-8"},{"row":138,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-47"},"document_id":"77F68D30","chunk_id":"cryc9nwjktom8","chunk_label":"unnamed-chunk-9"},{"row":143,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-48"},"document_id":"77F68D30","chunk_id":"c11kjcel8rada","chunk_label":"unnamed-chunk-10"},{"row":153,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"cu5f4f0k460vm","chunk_label":"unnamed-chunk-11"},{"row":160,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-50"},"document_id":"77F68D30","chunk_id":"cr9sbhfkl0l7m","chunk_label":"unnamed-chunk-12"},{"row":164,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-51"},"document_id":"77F68D30","chunk_id":"c67fek8fh1vdj","chunk_label":"unnamed-chunk-13"},{"row":168,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"c0qzt99s3uz17","chunk_label":"unnamed-chunk-14"},{"row":182,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-53"},"document_id":"77F68D30","chunk_id":"cf9t8qccs9es8","chunk_label":"unnamed-chunk-15"},{"row":190,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-54"},"document_id":"77F68D30","chunk_id":"c9ajju6cf9cv8","chunk_label":"unnamed-chunk-16"},{"row":197,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-55"},"document_id":"77F68D30","chunk_id":"cra8e128kr9o9","chunk_label":"unnamed-chunk-17"},{"row":202,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-56"},"document_id":"77F68D30","chunk_id":"cuafhnftbidp9","chunk_label":"unnamed-chunk-18"},{"row":210,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-57"},"document_id":"77F68D30","chunk_id":"cy1yzqk9v6smc","chunk_label":"unnamed-chunk-19"},{"row":237,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-58"},"document_id":"77F68D30","chunk_id":"c2fn3g4qdmzyy","chunk_label":"unnamed-chunk-20"},{"row":243,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-59"},"document_id":"77F68D30","chunk_id":"c85mkxl3u2xgh","chunk_label":"unnamed-chunk-21"},{"row":284,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-60"},"document_id":"77F68D30","chunk_id":"c2jly3c97ig8i","chunk_label":"unnamed-chunk-22"},{"row":304,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-61"},"document_id":"77F68D30","chunk_id":"cefh5ihzkcqbl","chunk_label":"unnamed-chunk-23"},{"row":313,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-62"},"document_id":"77F68D30","chunk_id":"cilpuhvqdmpej","chunk_label":"unnamed-chunk-24"},{"row":336,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-63"},"document_id":"77F68D30","chunk_id":"czxt33feq8xzf","chunk_label":"unnamed-chunk-25"},{"row":352,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-64"},"document_id":"77F68D30","chunk_id":"c63m2qhze3u2y","chunk_label":"unnamed-chunk-26"},{"row":361,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-20"},"document_id":"77F68D30","chunk_id":"c1tfzdz2b80hy","chunk_label":"unnamed-chunk-27"},{"row":368,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-61"},"document_id":"77F68D30","chunk_id":"c211ibf7ouri8","chunk_label":"unnamed-chunk-28"},{"row":398,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-2"},"document_id":"77F68D30","chunk_id":"cwqxet0zexx43","chunk_label":"unnamed-chunk-29"},{"row":464,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-4"},"document_id":"77F68D30","chunk_id":"cp6949hwjcirq","chunk_label":"unnamed-chunk-33"},{"row":481,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-23"},"document_id":"77F68D30","chunk_id":"c52kj3limvhya","chunk_label":"unnamed-chunk-34"},{"row":491,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-26"},"document_id":"77F68D30","chunk_id":"c4d2y4qjjl0e3","chunk_label":"unnamed-chunk-35"},{"row":504,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-28"},"document_id":"77F68D30","chunk_id":"cfk35uawz2hsn","chunk_label":"unnamed-chunk-36"},{"row":520,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-29"},"document_id":"77F68D30","chunk_id":"czw0rd1u0m33e","chunk_label":"unnamed-chunk-37"},{"row":538,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-30"},"document_id":"77F68D30","chunk_id":"cowzykbnv5xxz","chunk_label":"unnamed-chunk-38"},{"row":558,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-31"},"document_id":"77F68D30","chunk_id":"cct9s4x1m0a0t","chunk_label":"unnamed-chunk-39"},{"row":564,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-32"},"document_id":"77F68D30","chunk_id":"cozvs9s6hxsfi","chunk_label":"unnamed-chunk-40"},{"row":574,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-34"},"document_id":"77F68D30","chunk_id":"cwztp6gqf9w0s","chunk_label":"unnamed-chunk-41"},{"row":583,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-35"},"document_id":"77F68D30","chunk_id":"csmeeql20tusx","chunk_label":"unnamed-chunk-42"},{"row":608,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-68"},"document_id":"77F68D30","chunk_id":"cqa1e485jcrzr","chunk_label":"unnamed-chunk-43"},{"row":649,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","message":false,"label":"unnamed-chunk-94"},"document_id":"77F68D30","chunk_id":"cgq5kq1n6i5pg","chunk_label":"unnamed-chunk-45"},{"row":662,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-83"},"document_id":"77F68D30","chunk_id":"cuugb0843jsqr","chunk_label":"unnamed-chunk-47"},{"row":434,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-37"},"document_id":"77F68D30","chunk_id":"c69x3euxyb1pe","chunk_label":"unnamed-chunk-30"},{"row":446,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","fig.height":3.5,"fig.width":6.5,"label":"unnamed-chunk-65"},"document_id":"77F68D30","chunk_id":"cdj7r7sin50xc","chunk_label":"unnamed-chunk-31"},{"row":14,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-10"},"document_id":"77F68D30","chunk_id":"c5ns7cfqmp4sr","chunk_label":"unnamed-chunk-1"},{"row":658,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-68"},"document_id":"77F68D30","chunk_id":"cqa1e485jcrzr","chunk_label":"unnamed-chunk-46"},{"row":454,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-71"},"document_id":"77F68D30","chunk_id":"c8e0bbjupr09b","chunk_label":"unnamed-chunk-32"}],"doc_write_time":1674187233,"chunk_rendered_width":661} \ No newline at end of file +{"chunk_definitions":[{"row":15,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-24"},"document_id":"77F68D30","chunk_id":"c5ns7cfqmp4sr","chunk_label":"unnamed-chunk-1"},{"row":57,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-40"},"document_id":"77F68D30","chunk_id":"c0jrftmj331il","chunk_label":"unnamed-chunk-2"},{"row":63,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-41"},"document_id":"77F68D30","chunk_id":"cnq8c446n0wtz","chunk_label":"unnamed-chunk-3"},{"row":77,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-42"},"document_id":"77F68D30","chunk_id":"c3qooae84c4xs","chunk_label":"unnamed-chunk-4"},{"row":97,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-43"},"document_id":"77F68D30","chunk_id":"c8k36ylh3ik2z","chunk_label":"unnamed-chunk-5"},{"row":103,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-44"},"document_id":"77F68D30","chunk_id":"cff5zae4b4ix2","chunk_label":"unnamed-chunk-6"},{"row":362,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-20"},"document_id":"77F68D30","chunk_id":"c1tfzdz2b80hy","chunk_label":"unnamed-chunk-27"},{"row":435,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-37"},"document_id":"77F68D30","chunk_id":"c69x3euxyb1pe","chunk_label":"unnamed-chunk-30"},{"row":809,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-89"},"document_id":"77F68D30","chunk_id":"crdz8pq0jk07z","chunk_label":"unnamed-chunk-52"},{"row":687,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"csirmd4u6rxjw","chunk_label":"unnamed-chunk-50"},{"row":683,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-51"},"document_id":"77F68D30","chunk_id":"c7jwe67ih7e5t","chunk_label":"unnamed-chunk-49"},{"row":584,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-35"},"document_id":"77F68D30","chunk_id":"csmeeql20tusx","chunk_label":"unnamed-chunk-42"},{"row":663,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-83"},"document_id":"77F68D30","chunk_id":"cuugb0843jsqr","chunk_label":"unnamed-chunk-47"},{"row":650,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","message":false,"label":"unnamed-chunk-94"},"document_id":"77F68D30","chunk_id":"cgq5kq1n6i5pg","chunk_label":"unnamed-chunk-45"},{"row":117,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-45"},"document_id":"77F68D30","chunk_id":"cuj4f1k7oy0f9","chunk_label":"unnamed-chunk-7"},{"row":123,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-25"},"document_id":"77F68D30","chunk_id":"c3b4hkzi6i1gq","chunk_label":"unnamed-chunk-8"},{"row":575,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-34"},"document_id":"77F68D30","chunk_id":"cwztp6gqf9w0s","chunk_label":"unnamed-chunk-41"},{"row":565,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-32"},"document_id":"77F68D30","chunk_id":"cozvs9s6hxsfi","chunk_label":"unnamed-chunk-40"},{"row":139,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-47"},"document_id":"77F68D30","chunk_id":"cryc9nwjktom8","chunk_label":"unnamed-chunk-9"},{"row":144,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-48"},"document_id":"77F68D30","chunk_id":"c11kjcel8rada","chunk_label":"unnamed-chunk-10"},{"row":154,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"cu5f4f0k460vm","chunk_label":"unnamed-chunk-11"},{"row":161,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-50"},"document_id":"77F68D30","chunk_id":"cr9sbhfkl0l7m","chunk_label":"unnamed-chunk-12"},{"row":165,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-51"},"document_id":"77F68D30","chunk_id":"c67fek8fh1vdj","chunk_label":"unnamed-chunk-13"},{"row":169,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"c0qzt99s3uz17","chunk_label":"unnamed-chunk-14"},{"row":183,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"c0qzt99s3uz17","chunk_label":"unnamed-chunk-15"},{"row":191,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"c0qzt99s3uz17","chunk_label":"unnamed-chunk-16"},{"row":198,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"c0qzt99s3uz17","chunk_label":"unnamed-chunk-17"},{"row":559,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-8"},"document_id":"77F68D30","chunk_id":"cct9s4x1m0a0t","chunk_label":"unnamed-chunk-39"},{"row":539,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-7"},"document_id":"77F68D30","chunk_id":"cowzykbnv5xxz","chunk_label":"unnamed-chunk-38"},{"row":521,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-6"},"document_id":"77F68D30","chunk_id":"czw0rd1u0m33e","chunk_label":"unnamed-chunk-37"},{"row":505,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-49"},"document_id":"77F68D30","chunk_id":"cp6949hwjcirq","chunk_label":"unnamed-chunk-36"},{"row":492,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-49"},"document_id":"77F68D30","chunk_id":"cp6949hwjcirq","chunk_label":"unnamed-chunk-35"},{"row":482,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-49"},"document_id":"77F68D30","chunk_id":"cp6949hwjcirq","chunk_label":"unnamed-chunk-34"},{"row":465,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-49"},"document_id":"77F68D30","chunk_id":"cp6949hwjcirq","chunk_label":"unnamed-chunk-33"},{"row":455,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","fig.height":3.5,"fig.width":6.5,"label":"unnamed-chunk-65"},"document_id":"77F68D30","chunk_id":"cdj7r7sin50xc","chunk_label":"unnamed-chunk-32"},{"row":447,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","fig.height":3.5,"fig.width":6.5,"label":"unnamed-chunk-65"},"document_id":"77F68D30","chunk_id":"cdj7r7sin50xc","chunk_label":"unnamed-chunk-31"},{"row":399,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-2"},"document_id":"77F68D30","chunk_id":"cwqxet0zexx43","chunk_label":"unnamed-chunk-29"},{"row":369,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-59"},"document_id":"77F68D30","chunk_id":"c85mkxl3u2xgh","chunk_label":"unnamed-chunk-28"},{"row":353,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-59"},"document_id":"77F68D30","chunk_id":"c85mkxl3u2xgh","chunk_label":"unnamed-chunk-26"},{"row":337,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-59"},"document_id":"77F68D30","chunk_id":"c85mkxl3u2xgh","chunk_label":"unnamed-chunk-25"},{"row":314,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-59"},"document_id":"77F68D30","chunk_id":"c85mkxl3u2xgh","chunk_label":"unnamed-chunk-24"},{"row":305,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-59"},"document_id":"77F68D30","chunk_id":"c85mkxl3u2xgh","chunk_label":"unnamed-chunk-23"},{"row":285,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-59"},"document_id":"77F68D30","chunk_id":"c85mkxl3u2xgh","chunk_label":"unnamed-chunk-22"},{"row":244,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-59"},"document_id":"77F68D30","chunk_id":"c85mkxl3u2xgh","chunk_label":"unnamed-chunk-21"},{"row":238,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-59"},"document_id":"77F68D30","chunk_id":"c85mkxl3u2xgh","chunk_label":"unnamed-chunk-20"},{"row":211,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"c0qzt99s3uz17","chunk_label":"unnamed-chunk-19"},{"row":203,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-52"},"document_id":"77F68D30","chunk_id":"c0qzt99s3uz17","chunk_label":"unnamed-chunk-18"},{"row":824,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-92"},"document_id":"77F68D30","chunk_id":"cxaye4qsd68kd","chunk_label":"unnamed-chunk-55"},{"row":819,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-91"},"document_id":"77F68D30","chunk_id":"csibvqbb2q9r9","chunk_label":"unnamed-chunk-54"},{"row":669,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-50"},"document_id":"77F68D30","chunk_id":"czdkbdsep34zc","chunk_label":"unnamed-chunk-48"},{"row":815,"row_count":1,"visible":true,"expansion_state":0,"options":{"engine":"r","label":"unnamed-chunk-90"},"document_id":"77F68D30","chunk_id":"cvya5ncyop03f","chunk_label":"unnamed-chunk-53"}],"doc_write_time":1674959533,"chunk_rendered_width":661} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cilpuhvqdmpej/000013.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cilpuhvqdmpej/000013.csv deleted file mode 100644 index 12da45b..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cilpuhvqdmpej/000013.csv +++ /dev/null @@ -1,5 +0,0 @@ -"0","n <- 1e3" -"0","a <- rnorm(n,0,10)" -"0","b <- runif(n,0,1)" -"0","plot(NULL,xlim=c(130,170),ylim=c(50,90)," -"0"," xlab=""height(cm)"", ylab=""weight(kg)"",)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cilpuhvqdmpej/000015.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cilpuhvqdmpej/000015.csv deleted file mode 100644 index cf6a98f..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cilpuhvqdmpej/000015.csv +++ /dev/null @@ -1 +0,0 @@ -"0","for (j in 1:50 ) abline(a=a[j], b=b[j], lwd=2, col=2)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cilpuhvqdmpej/000016.metadata b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cilpuhvqdmpej/000016.metadata deleted file mode 100644 index 6399179..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cilpuhvqdmpej/000016.metadata +++ /dev/null @@ -1 +0,0 @@ -{"height":408.5290482076638,"width":661.0,"size_behavior":0,"conditions":[]} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cilpuhvqdmpej/000016.png b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cilpuhvqdmpej/000016.png deleted file mode 100644 index d043d37..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cilpuhvqdmpej/000016.png and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cilpuhvqdmpej/000016.snapshot b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cilpuhvqdmpej/000016.snapshot deleted file mode 100644 index 8b5ca9a..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cilpuhvqdmpej/000016.snapshot and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cowzykbnv5xxz/000010.metadata b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cowzykbnv5xxz/000010.metadata deleted file mode 100644 index 5487c37..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cowzykbnv5xxz/000010.metadata +++ /dev/null @@ -1 +0,0 @@ -{"classes":["data.frame"],"nrow":3,"ncol":4,"summary":{"Description":["df [3 × 4]"]}} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cowzykbnv5xxz/000010.rdf b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cowzykbnv5xxz/000010.rdf deleted file mode 100644 index 329f720..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cowzykbnv5xxz/000010.rdf and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cowzykbnv5xxz/00000f.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cowzykbnv5xxz/000011.csv similarity index 100% rename from .Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cowzykbnv5xxz/00000f.csv rename to .Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cowzykbnv5xxz/000011.csv diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cct9s4x1m0a0t/000010.metadata b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cowzykbnv5xxz/000012.metadata similarity index 100% rename from .Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cct9s4x1m0a0t/000010.metadata rename to .Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cowzykbnv5xxz/000012.metadata diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cowzykbnv5xxz/000012.rdf b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cowzykbnv5xxz/000012.rdf new file mode 100644 index 0000000..c40a831 Binary files /dev/null and b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cowzykbnv5xxz/000012.rdf differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cp6949hwjcirq/00000f.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cp6949hwjcirq/00000f.csv index 2b210f3..0ba56eb 100644 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cp6949hwjcirq/00000f.csv +++ b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cp6949hwjcirq/00000f.csv @@ -1,26 +1,3 @@ -"0","library(rethinking)" -"0","library(tidyverse)" -"2","Registered S3 methods overwritten by 'dbplyr': - method from - print.tbl_lazy - print.tbl_sql -" -"2","── Attaching packages ─────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse 1.3.2 ──" -"2","✔ ggplot2 3.4.0 ✔ purrr  0.3.5 -✔ tibble  3.1.8 ✔ dplyr  1.0.10 -✔ tidyr  1.2.1 ✔ stringr 1.4.1 -✔ readr  2.1.3 ✔ forcats 0.5.2 " -"2","Warning: package ‘ggplot2’ was built under R version 4.2.2" -"2","Warning: package ‘tidyr’ was built under R version 4.2.2" -"2","Warning: package ‘readr’ was built under R version 4.2.2" -"2","Warning: package ‘purrr’ was built under R version 4.2.2" -"2","Warning: package ‘dplyr’ was built under R version 4.2.2" -"2","── Conflicts ────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── tidyverse_conflicts() ── -✖ tidyr::extract() masks rstan::extract() -✖ dplyr::filter() masks stats::filter() -✖ dplyr::lag() masks stats::lag() -✖ purrr::map() masks rethinking::map()" -"0","library(dplyr)" -"0","" "0","data(Howell1)" -"0","d <-Howell1[Howell1$age>=18,]" +"0","# d <-Howell1[Howell1$age>=18,]" +"0","d <-Howell1" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cpmm75wbk2abb/00000f.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cpmm75wbk2abb/00000f.csv new file mode 100644 index 0000000..567f461 --- /dev/null +++ b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cpmm75wbk2abb/00000f.csv @@ -0,0 +1,40 @@ +"0","# Same Model in STAN" +"0","stan_m4.5 <- """ +"0","data {" +"0"," int N;" +"0"," vector height[N];" +"0"," vector weight_s[N];" +"0"," vector weight_s2[N];" +"0","}" +"0","parameters {" +"0"," real a, b1, b2;" +"0"," real sigma;" +"0","}" +"0","" +"0","transformed parameters {" +"0"," vector[N] mu = -a+b1*weight_s+b2*weight_s2;" +"0","}" +"0","" +"0","model {" +"0"," // priors" +"0"," a ~normal(178,20);" +"0"," b1 ~lognormal(0,1);" +"0"," b2 ~normal(0,1);" +"0"," sigma ~unif(0,50);" +"0"," " +"0"," // model " +"0"," height ~normal(mu,sigma);" +"0","}""" +"0","" +"0","data_m4.5 <- list(" +"0"," N = nrow(d)," +"0"," height = d$height," +"0"," weight_s = d$weight_s," +"0"," weight_s2 = d$weight_s2" +"0",")" +"0","" +"0","fit_m4.5 <- stan(data = data_m4.5, mode_code = stan_m4.5, " +"0"," iter = 1000, verbose = FALSE)" +"2","Error in cmdstan_model(file, compile = TRUE, cpp_options = cpp_options) : + argument ""file"" is missing, with no default +" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cpmm75wbk2abb/000010.error b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cpmm75wbk2abb/000010.error new file mode 100644 index 0000000..fb3d132 --- /dev/null +++ b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cpmm75wbk2abb/000010.error @@ -0,0 +1 @@ +{"frames":[{"func":"stan(data = data_m4.5, mode_code = stan_m4.5, iter = 1000, verbose = FALSE)","file":"","line_number":0,"end_line_number":0,"character_number":0,"end_character_number":0},{"func":"cstan(...)","file":"","line_number":0,"end_line_number":0,"character_number":0,"end_character_number":0},{"func":"cmdstan_model(file, compile = TRUE, cpp_options = cpp_options)","file":"","line_number":0,"end_line_number":0,"character_number":0,"end_character_number":0}],"message":"Error in cmdstan_model(file, compile = TRUE, cpp_options = cpp_options) : \n argument \"file\" is missing, with no default\n"} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cqa1e485jcrzr/000048.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cqa1e485jcrzr/000048.csv deleted file mode 100644 index cf7cff8..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cqa1e485jcrzr/000048.csv +++ /dev/null @@ -1,3 +0,0 @@ -"0","post <- extract.samples(m_SHW)" -"0","dens(post$b[,1],xlim=c(.3, .9),ylim=c(0,8)," -"0"," lwd=3,col=2,xlab=""posterior mean weight(kg)"")" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cqa1e485jcrzr/00004a.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cqa1e485jcrzr/00004a.csv deleted file mode 100644 index 26bdb3c..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cqa1e485jcrzr/00004a.csv +++ /dev/null @@ -1 +0,0 @@ -"0","dens(post$b[,2],lwd=3,col=4,add=TRUE)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cqa1e485jcrzr/00004b.metadata b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cqa1e485jcrzr/00004b.metadata deleted file mode 100644 index 6399179..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cqa1e485jcrzr/00004b.metadata +++ /dev/null @@ -1 +0,0 @@ -{"height":408.5290482076638,"width":661.0,"size_behavior":0,"conditions":[]} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cqa1e485jcrzr/00004b.png b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cqa1e485jcrzr/00004b.png deleted file mode 100644 index 29162ba..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cqa1e485jcrzr/00004b.png and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cqa1e485jcrzr/00004b.snapshot b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cqa1e485jcrzr/00004b.snapshot deleted file mode 100644 index fc0cb52..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cqa1e485jcrzr/00004b.snapshot and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cra8e128kr9o9/00000f.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cra8e128kr9o9/00000f.csv deleted file mode 100644 index 4b6e2e2..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cra8e128kr9o9/00000f.csv +++ /dev/null @@ -1,2 +0,0 @@ -"0","library(rethinking)" -"0","post_samples <- rbeta(1e3, 6+1, 3+1)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/crdz8pq0jk07z/00002d.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/crdz8pq0jk07z/00002d.csv new file mode 100644 index 0000000..a935254 --- /dev/null +++ b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/crdz8pq0jk07z/00002d.csv @@ -0,0 +1,1223 @@ +"0","# Same Model in STAN" +"0","stan_m4.6 <- """ +"0","data {" +"0"," int N;" +"0"," vector[N] height;" +"0"," vector[N] weight_s;" +"0"," vector[N] weight_s2;" +"0","}" +"0","" +"0","transformed data{" +"0"," real bar_x1;" +"0"," real x1_sd;" +"0"," vector[N] x1_std;" +"0"," real y_sd;" +"0","" +"0"," bar_x1 = mean(weight_s);" +"0"," x1_sd = sd(weight_s);" +"0"," x1_std = (weight_s - bar_x1)/x1_sd; // centered and scaled" +"0","" +"0"," y_sd = sd(height);" +"0","}" +"0","" +"0","parameters {" +"0"," real alpha_std;" +"0"," real beta1_std;" +"0"," real beta3_std;" +"0"," real sigma;" +"0","}" +"0","" +"0","transformed parameters {" +"0"," vector[N] mu;" +"0"," " +"0"," for (i in 1:N)" +"0"," mu[i] = alpha_std + beta1_std*weight_s[i] + " +"0"," beta3_std*weight_s2[i];" +"0","}" +"0","" +"0","model {" +"0"," // priors" +"0"," alpha_std ~ normal(0,20);" +"0"," beta1_std ~ normal(0,2);" +"0"," beta3_std ~ normal(0,2);" +"0"," sigma ~ normal(5,1.5);" +"0"," // sigma ~ exponential(1/y_sd); " +"0"," " +"0"," // model " +"0"," height ~normal(mu,sigma);" +"0","}" +"0","" +"0","generated quantities {" +"0"," real alpha;" +"0"," real beta1;" +"0"," real beta3;" +"0","" +"0"," alpha = alpha_std - (beta1_std*bar_x1)/x1_sd " +"0"," + (beta3_std*bar_x1^2)/x1_sd^2;" +"0","" +"0"," beta1 = beta1_std/x1_sd - 2*(beta3_std*bar_x1)/x1_sd^2;" +"0","" +"0"," beta3 = beta3_std/x1_sd^2;" +"0","}""" +"0","" +"0","data_m4.6 <- list(" +"0"," N = nrow(d)," +"0"," height = d$height," +"0"," weight_s = d$weight_s," +"0"," weight_s2 = d$weight_s2" +"0",")" +"0","" +"0","fit_m4.6 <- stan(data = data_m4.6, " +"0"," model_code = stan_m4.6," +"0"," chains = 4," +"0"," iter = 2000)" +"2","Compiling Stan program... +" +"1"," " +"1","" +"1","-" +"1","" +"1"," " +"1"," " +"1","" +"1","\" +"1","" +"1"," " +"1"," " +"1","" +"1","|" +"1","" +"1"," " +"1"," " +"1","" +"1","/" +"1","" +"1"," " +"1"," " +"1","" +"1","-" +"1","" +"1"," " +"1"," " +"1","" +"1","\" +"1","" +"1"," " +"1"," " +"1","" +"1","|" +"1","" +"1"," " +"1"," " +"1","" +"1","/" +"1","" +"1"," " +"1"," " +"1","" +"1","-" +"1","" +"1"," " +"1"," " +"1","" +"1","\" +"1","" +"1"," " +"1"," " +"1","" +"1","|" +"1","" +"1"," " +"1"," " +"1","" +"1","/" +"1","" +"1"," " +"1"," " +"1","" +"1","-" +"1","" +"1"," " +"1"," " +"1","" +"1","\" +"1","" +"1"," " +"1"," " +"1","" +"1","|" +"1","" +"1"," " +"1"," " +"1","" +"1","/" +"1","" +"1"," " +"1"," " +"1","" +"1","-" +"1","" +"1"," " +"1"," " +"1","" +"1","\" +"1","" +"1"," " +"1"," " +"1","" +"1","|" +"1","" +"1"," " +"1"," " +"1","" +"1","/" +"1","" +"1"," " +"1"," " +"1","" +"1","-" +"1","" +"1"," " +"1"," " +"1","" +"1","\" +"1","" +"1"," " +"1"," " +"1","" +"1","|" +"1","" +"1"," " +"1"," " +"1","" +"1","/" +"1","" +"1"," " +"1"," " +"1","" +"1","-" +"1","" +"1"," " +"1"," " +"1","" +"1","\" +"1","" +"1"," " +"1"," " +"1","" +"1","|" +"1","" +"1"," " +"1"," " +"1","" +"1","/" +"1","" +"1"," " +"1"," " +"1","" +"1","-" +"1","" +"1"," " +"1"," " +"1","" +"1","\" +"1","" +"1"," " +"1"," " +"1","" +"1","|" +"1","" +"1"," " +"1"," " +"1","" +"1","/" +"1","" +"1"," " +"1"," " +"1","" +"1","-" +"1","" +"1"," " +"1"," " +"1","" +"1","\" +"1","" +"1"," " +"1"," " +"1","" +"1","|" +"1","" +"1"," " +"1"," " +"1","" +"1","/" +"1","" +"1"," " +"1"," " +"1","" +"1","-" +"1","" +"1"," " +"1"," " +"1","" +"1","\" +"1","" +"1"," " +"1"," " +"1","" +"1","|" +"1","" +"1"," " +"1"," " +"1","" +"1","/" +"1","" +"1"," " +"1"," " +"1","" +"1","-" +"1","" +"1"," " +"1"," " +"1","" +"1","\" +"1","" +"1"," " +"1"," " +"1","" +"1","|" +"1","" +"1"," " +"1"," " +"1","" +"1","/" +"1","" +"1"," " +"1"," " +"1","" +"1","-" +"1","" +"1"," " +"1"," " +"1","" +"1","\" +"1","" +"1"," " +"1"," " +"1","" +"1","|" +"1","" +"1"," " +"1"," " +"1","" +"1","/" +"1","" +"1"," " +"1"," " +"1","" +"1","-" +"1","" +"1"," " +"1"," " +"1","" +"1","\" +"1","" +"1"," " +"1"," " +"1","" +"1","|" +"1","" +"1"," " +"1"," " +"1","" +"1","/" +"1","" +"1"," " +"1"," " +"1","" +"1","-" +"1","" +"1"," " +"1"," " +"1","" +"1","\" +"1","" +"1"," " +"1"," " +"1","" +"1","|" +"1","" +"1"," " +"1"," " +"1","" +"1","/" +"1","" +"1"," " +"1"," " +"1","" +"1","-" +"1","" +"1"," " +"1"," " +"1","" +"1","\" +"1","" +"1"," " +"1"," " +"1","" +"1","|" +"1","" +"1"," " +"1"," " +"1","" +"1","/" +"1","" +"1"," " +"1"," " +"1","" +"1","-" +"1","" +"1"," " +"1"," " +"1","" +"1","\" +"1","" +"1"," " +"1"," " +"1","" +"1","|" +"1","" +"1"," " +"1"," " +"1","" +"1","/" +"1","" +"1"," " +"1"," " +"1","" +"1","-" +"1","" +"1"," " +"1"," " +"1","" +"1","\" +"1","" +"1"," " +"1"," " +"1","" +"1","|" +"1","" +"1"," " +"1"," " +"1","" +"1","/" +"1","" +"1"," " +"1"," " +"1","" +"1","-" +"1","" +"1"," " +"1"," " +"1","" +"1","\" +"1","" +"1"," " +"1"," " +"1","" +"1","|" +"1","" +"1"," " +"1"," " +"1","" +"1","/" +"1","" +"1"," " +"1"," " +"1","" +"1","-" +"1","" +"1"," " +"1"," " +"1","" +"1","\" +"1","" +"1"," " +"1"," " +"1","" +"1","|" +"1","" +"1"," " +"1"," " +"1","" +"1","/" +"1","" +"1"," " +"1"," " +"1","Running MCMC with 4 sequential chains... + +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 1 / 2000 [ 0%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 100 / 2000 [ 5%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 200 / 2000 [ 10%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 300 / 2000 [ 15%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 400 / 2000 [ 20%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 500 / 2000 [ 25%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 600 / 2000 [ 30%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 700 / 2000 [ 35%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 800 / 2000 [ 40%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 900 / 2000 [ 45%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 1000 / 2000 [ 50%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 1001 / 2000 [ 50%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 1100 / 2000 [ 55%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 1200 / 2000 [ 60%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 1300 / 2000 [ 65%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 1400 / 2000 [ 70%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 1500 / 2000 [ 75%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 1600 / 2000 [ 80%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 1700 / 2000 [ 85%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 1800 / 2000 [ 90%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 1900 / 2000 [ 95%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","Iteration: 2000 / 2000 [100%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","1" +"1"," " +"1","finished in" +"1"," " +"1","2.0" +"1"," " +"1","seconds. +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 1 / 2000 [ 0%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 100 / 2000 [ 5%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 200 / 2000 [ 10%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 300 / 2000 [ 15%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 400 / 2000 [ 20%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 500 / 2000 [ 25%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 600 / 2000 [ 30%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 700 / 2000 [ 35%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 800 / 2000 [ 40%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 900 / 2000 [ 45%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 1000 / 2000 [ 50%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 1001 / 2000 [ 50%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 1100 / 2000 [ 55%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 1200 / 2000 [ 60%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 1300 / 2000 [ 65%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 1400 / 2000 [ 70%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 1500 / 2000 [ 75%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 1600 / 2000 [ 80%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 1700 / 2000 [ 85%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 1800 / 2000 [ 90%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 1900 / 2000 [ 95%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","Iteration: 2000 / 2000 [100%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","2" +"1"," " +"1","finished in" +"1"," " +"1","2.0" +"1"," " +"1","seconds. +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 1 / 2000 [ 0%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 100 / 2000 [ 5%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 200 / 2000 [ 10%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 300 / 2000 [ 15%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 400 / 2000 [ 20%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 500 / 2000 [ 25%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 600 / 2000 [ 30%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 700 / 2000 [ 35%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 800 / 2000 [ 40%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 900 / 2000 [ 45%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 1000 / 2000 [ 50%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 1001 / 2000 [ 50%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 1100 / 2000 [ 55%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 1200 / 2000 [ 60%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 1300 / 2000 [ 65%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 1400 / 2000 [ 70%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 1500 / 2000 [ 75%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 1600 / 2000 [ 80%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 1700 / 2000 [ 85%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 1800 / 2000 [ 90%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 1900 / 2000 [ 95%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","Iteration: 2000 / 2000 [100%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","3" +"1"," " +"1","finished in" +"1"," " +"1","1.8" +"1"," " +"1","seconds. +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 1 / 2000 [ 0%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 100 / 2000 [ 5%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 200 / 2000 [ 10%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 300 / 2000 [ 15%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 400 / 2000 [ 20%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 500 / 2000 [ 25%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 600 / 2000 [ 30%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 700 / 2000 [ 35%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 800 / 2000 [ 40%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 900 / 2000 [ 45%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 1000 / 2000 [ 50%] (Warmup)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 1001 / 2000 [ 50%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 1100 / 2000 [ 55%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 1200 / 2000 [ 60%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 1300 / 2000 [ 65%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 1400 / 2000 [ 70%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 1500 / 2000 [ 75%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 1600 / 2000 [ 80%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 1700 / 2000 [ 85%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 1800 / 2000 [ 90%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 1900 / 2000 [ 95%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","Iteration: 2000 / 2000 [100%] (Sampling)" +"1"," " +"1"," +" +"1","Chain" +"1"," " +"1","4" +"1"," " +"1","finished in" +"1"," " +"1","1.9" +"1"," " +"1","seconds. +" +"1"," +All" +"1"," " +"1","4" +"1"," " +"1","chains finished successfully. +" +"1","Mean chain execution time:" +"1"," " +"1","1.9" +"1"," " +"1","seconds. +" +"1","Total execution time:" +"1"," " +"1","8.2" +"1"," " +"1","seconds. + +" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/csibvqbb2q9r9/00001f.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/csibvqbb2q9r9/00001f.csv new file mode 100644 index 0000000..99d0500 --- /dev/null +++ b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/csibvqbb2q9r9/00001f.csv @@ -0,0 +1 @@ +"0","traceplot(fit_m4.6, pars = ""sigma"")" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c211ibf7ouri8/000037.metadata b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/csibvqbb2q9r9/000020.metadata similarity index 100% rename from .Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c211ibf7ouri8/000037.metadata rename to .Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/csibvqbb2q9r9/000020.metadata diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/csibvqbb2q9r9/000020.png b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/csibvqbb2q9r9/000020.png new file mode 100644 index 0000000..2a172e8 Binary files /dev/null and b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/csibvqbb2q9r9/000020.png differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/csibvqbb2q9r9/000020.snapshot b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/csibvqbb2q9r9/000020.snapshot new file mode 100644 index 0000000..f715602 Binary files /dev/null and b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/csibvqbb2q9r9/000020.snapshot differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/csirmd4u6rxjw/000013.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/csirmd4u6rxjw/000013.csv new file mode 100644 index 0000000..bb49894 --- /dev/null +++ b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/csirmd4u6rxjw/000013.csv @@ -0,0 +1 @@ +"0","precis(m4.5)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001e.metadata b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/csirmd4u6rxjw/000014.metadata similarity index 74% rename from .Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001e.metadata rename to .Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/csirmd4u6rxjw/000014.metadata index 6694fae..9d1e550 100644 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c63m2qhze3u2y/00001e.metadata +++ b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/csirmd4u6rxjw/000014.metadata @@ -1 +1 @@ -{"classes":["data.frame"],"nrow":6,"ncol":3,"summary":{"Description":["df [6 × 3]"]}} \ No newline at end of file +{"classes":["data.frame"],"nrow":4,"ncol":4,"summary":{"Description":["df [4 × 4]"]}} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/csirmd4u6rxjw/000014.rdf b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/csirmd4u6rxjw/000014.rdf new file mode 100644 index 0000000..99a1645 Binary files /dev/null and b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/csirmd4u6rxjw/000014.rdf differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cuafhnftbidp9/000013.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cuafhnftbidp9/000013.csv deleted file mode 100644 index 050a726..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cuafhnftbidp9/000013.csv +++ /dev/null @@ -1 +0,0 @@ -"0","dens(post_samples, lwd=4, col=2, xlab=""proportion water"", adj=.1)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cuafhnftbidp9/000015.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cuafhnftbidp9/000015.csv deleted file mode 100644 index 1a40f36..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cuafhnftbidp9/000015.csv +++ /dev/null @@ -1 +0,0 @@ -"0","curve(dbeta(x, 6+1, 3+1), add=TRUE, lty=2, lwd=3)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cuafhnftbidp9/000016.metadata b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cuafhnftbidp9/000016.metadata deleted file mode 100644 index 6399179..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cuafhnftbidp9/000016.metadata +++ /dev/null @@ -1 +0,0 @@ -{"height":408.5290482076638,"width":661.0,"size_behavior":0,"conditions":[]} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cuafhnftbidp9/000016.png b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cuafhnftbidp9/000016.png deleted file mode 100644 index 1031f6b..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cuafhnftbidp9/000016.png and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cuafhnftbidp9/000016.snapshot b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cuafhnftbidp9/000016.snapshot deleted file mode 100644 index 5a12c55..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cuafhnftbidp9/000016.snapshot and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cvya5ncyop03f/000017.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cvya5ncyop03f/000017.csv new file mode 100644 index 0000000..99829bf --- /dev/null +++ b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cvya5ncyop03f/000017.csv @@ -0,0 +1,237 @@ +"0","print(fit_m4.6, pars = c(""alpha"",""beta1"", ""beta3"", ""sigma""))" +"1","Inference for Stan model: " +"1","" +"1","rt_cmdstanr_28862db112abe434af40140009da8c75-202301290241-1-5c9c61" +"1","" +"1",". +" +"1","4" +"1","" +"1"," chains, each with iter=" +"1","" +"1","2000" +"1","" +"1","; warmup=" +"1","" +"1","1000" +"1","" +"1","; thin=" +"1","" +"1","1" +"1","" +"1","; +" +"1","" +"1","post-warmup draws per chain=" +"1","" +"1","1000" +"1","" +"1",", " +"1","" +"1","total post-warmup draws=" +"1","" +"1","4000" +"1","" +"1",". + +" +"1"," " +"1"," mean" +"1"," se_mean" +"1"," sd" +"1"," 2.5%" +"1"," 25%" +"1"," 50%" +"1"," 75%" +"1"," 97.5%" +"1"," n_eff" +"1"," Rhat" +"1"," +alpha" +"1"," 146.66" +"1"," 0.01" +"1"," 0.37" +"1"," 145.92" +"1"," 146.41" +"1"," 146.66" +"1"," 146.92" +"1"," 147.38" +"1"," 1628" +"1"," 1" +"1"," +beta1" +"1"," 21.09" +"1"," 0.01" +"1"," 0.29" +"1"," 20.51" +"1"," 20.89" +"1"," 21.09" +"1"," 21.29" +"1"," 21.63" +"1"," 1998" +"1"," 1" +"1"," +beta3" +"1"," -8.44" +"1"," 0.01" +"1"," 0.28" +"1"," -8.99" +"1"," -8.63" +"1"," -8.43" +"1"," -8.24" +"1"," -7.88" +"1"," 1562" +"1"," 1" +"1"," +sigma" +"1"," 5.78" +"1"," 0.00" +"1"," 0.18" +"1"," 5.45" +"1"," 5.66" +"1"," 5.77" +"1"," 5.89" +"1"," 6.13" +"1"," 2393" +"1"," 1" +"1"," +" +"1"," +Samples were drawn using " +"1","" +"1","NUTS(diag_e)" +"1","" +"1"," at " +"1","" +"1","Sun Jan 29 2:42:03 AM 2023" +"1","" +"1",". +" +"1","" +"1","For each parameter, n_eff is a crude measure of effective sample size, +" +"1","" +"1","and Rhat is the potential scale reduction factor on split chains (at +" +"1","" +"1","convergence, Rhat=1). +" +"0","print(fit_m4.6, pars = c(""alpha_std"",""beta1_std"", ""beta3_std"", ""sigma""))" +"1","Inference for Stan model: " +"1","" +"1","rt_cmdstanr_28862db112abe434af40140009da8c75-202301290241-1-5c9c61" +"1","" +"1",". +" +"1","4" +"1","" +"1"," chains, each with iter=" +"1","" +"1","2000" +"1","" +"1","; warmup=" +"1","" +"1","1000" +"1","" +"1","; thin=" +"1","" +"1","1" +"1","" +"1","; +" +"1","" +"1","post-warmup draws per chain=" +"1","" +"1","1000" +"1","" +"1",", " +"1","" +"1","total post-warmup draws=" +"1","" +"1","4000" +"1","" +"1",". + +" +"1"," " +"1"," mean" +"1"," se_mean" +"1"," sd" +"1"," 2.5%" +"1"," 25%" +"1"," 50%" +"1"," 75%" +"1"," 97.5%" +"1"," n_eff" +"1"," Rhat" +"1"," +alpha_std" +"1"," 146.66" +"1"," 0.01" +"1"," 0.37" +"1"," 145.92" +"1"," 146.41" +"1"," 146.66" +"1"," 146.92" +"1"," 147.38" +"1"," 1628" +"1"," 1" +"1"," +beta1_std" +"1"," 21.09" +"1"," 0.01" +"1"," 0.29" +"1"," 20.51" +"1"," 20.89" +"1"," 21.09" +"1"," 21.29" +"1"," 21.63" +"1"," 1998" +"1"," 1" +"1"," +beta3_std" +"1"," -8.44" +"1"," 0.01" +"1"," 0.28" +"1"," -8.99" +"1"," -8.63" +"1"," -8.43" +"1"," -8.24" +"1"," -7.88" +"1"," 1562" +"1"," 1" +"1"," +sigma " +"1"," 5.78" +"1"," 0.00" +"1"," 0.18" +"1"," 5.45" +"1"," 5.66" +"1"," 5.77" +"1"," 5.89" +"1"," 6.13" +"1"," 2393" +"1"," 1" +"1"," +" +"1"," +Samples were drawn using " +"1","" +"1","NUTS(diag_e)" +"1","" +"1"," at " +"1","" +"1","Sun Jan 29 2:42:03 AM 2023" +"1","" +"1",". +" +"1","" +"1","For each parameter, n_eff is a crude measure of effective sample size, +" +"1","" +"1","and Rhat is the potential scale reduction factor on split chains (at +" +"1","" +"1","convergence, Rhat=1). +" +"0","precis(m4.5)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cvya5ncyop03f/000018.metadata b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cvya5ncyop03f/000018.metadata new file mode 100644 index 0000000..9d1e550 --- /dev/null +++ b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cvya5ncyop03f/000018.metadata @@ -0,0 +1 @@ +{"classes":["data.frame"],"nrow":4,"ncol":4,"summary":{"Description":["df [4 × 4]"]}} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cvya5ncyop03f/000018.rdf b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cvya5ncyop03f/000018.rdf new file mode 100644 index 0000000..0ac1f20 Binary files /dev/null and b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cvya5ncyop03f/000018.rdf differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cxaye4qsd68kd/000013.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cxaye4qsd68kd/000013.csv new file mode 100644 index 0000000..80bf245 --- /dev/null +++ b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cxaye4qsd68kd/000013.csv @@ -0,0 +1,2 @@ +"0","sigma_sample <- extract(fit_m4.6)[[""sigma""]]" +"0","hist(sigma_sample)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003c.metadata b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cxaye4qsd68kd/000014.metadata similarity index 100% rename from .Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003c.metadata rename to .Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cxaye4qsd68kd/000014.metadata diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cxaye4qsd68kd/000014.png b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cxaye4qsd68kd/000014.png new file mode 100644 index 0000000..3db1121 Binary files /dev/null and b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cxaye4qsd68kd/000014.png differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cxaye4qsd68kd/000014.snapshot b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cxaye4qsd68kd/000014.snapshot new file mode 100644 index 0000000..0399e5f Binary files /dev/null and b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cxaye4qsd68kd/000014.snapshot differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cy1yzqk9v6smc/000011.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cy1yzqk9v6smc/000011.csv deleted file mode 100644 index e526fa3..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cy1yzqk9v6smc/000011.csv +++ /dev/null @@ -1,5 +0,0 @@ -"0","w <-6;n<-9;" -"0","p_grid <-seq(from=0,to=1,length.out=100)" -"0","posterior <-dbinom(w,n,p_grid)*dunif(p_grid,0,1)" -"0","posterior <-posterior/sum(posterior)" -"0","plot(posterior,col=2, type=""l"")" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cy1yzqk9v6smc/000012.metadata b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cy1yzqk9v6smc/000012.metadata deleted file mode 100644 index 6399179..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cy1yzqk9v6smc/000012.metadata +++ /dev/null @@ -1 +0,0 @@ -{"height":408.5290482076638,"width":661.0,"size_behavior":0,"conditions":[]} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cy1yzqk9v6smc/000012.png b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cy1yzqk9v6smc/000012.png deleted file mode 100644 index e4e6bd8..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cy1yzqk9v6smc/000012.png and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cy1yzqk9v6smc/000012.snapshot b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cy1yzqk9v6smc/000012.snapshot deleted file mode 100644 index c8d7f60..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/cy1yzqk9v6smc/000012.snapshot and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czdkbdsep34zc/000005.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czdkbdsep34zc/000005.csv new file mode 100644 index 0000000..501a399 --- /dev/null +++ b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czdkbdsep34zc/000005.csv @@ -0,0 +1 @@ +"0","plot(height~weight,d)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003e.metadata b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czdkbdsep34zc/000006.metadata similarity index 100% rename from .Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/c52kj3limvhya/00003e.metadata rename to .Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czdkbdsep34zc/000006.metadata diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czdkbdsep34zc/000006.png b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czdkbdsep34zc/000006.png new file mode 100644 index 0000000..8bab57f Binary files /dev/null and b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czdkbdsep34zc/000006.png differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czdkbdsep34zc/000006.snapshot b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czdkbdsep34zc/000006.snapshot new file mode 100644 index 0000000..766c418 Binary files /dev/null and b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czdkbdsep34zc/000006.snapshot differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czw0rd1u0m33e/00000f.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czw0rd1u0m33e/00000f.csv index 4e2edfc..01a263d 100644 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czw0rd1u0m33e/00000f.csv +++ b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czw0rd1u0m33e/00000f.csv @@ -9,6 +9,6 @@ "0","# effect of sex (male-female)" "0","mean(simM$W-simF$W)" "1","[1]" -"1"," 21.24533" +"1"," 20.72138" "1"," " diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/000015.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/000015.csv deleted file mode 100644 index 4fbff77..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/000015.csv +++ /dev/null @@ -1,14 +0,0 @@ -"0","dat <- list(W=d$weight, H=d$height)" -"0","#k <- 10" -"0","#dat <- list(W=d$weight[1:k], H=d$height[1:k])" -"0","" -"0","#Run Model" -"0","m3.2 <- quap(" -"0"," alist(" -"0"," W ~ dnorm(mu,sigma)," -"0"," mu <- a + b*H," -"0"," a ~ dnorm(0,10)," -"0"," b ~ dunif(0,1)," -"0"," sigma~dunif(0,10)" -"0","),data=dat)" -"0","precis(m3.2)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/000016.metadata b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/000016.metadata deleted file mode 100644 index 5487c37..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/000016.metadata +++ /dev/null @@ -1 +0,0 @@ -{"classes":["data.frame"],"nrow":3,"ncol":4,"summary":{"Description":["df [3 × 4]"]}} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/000016.rdf b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/000016.rdf deleted file mode 100644 index 23ba9ae..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/000016.rdf and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/000017.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/000017.csv deleted file mode 100644 index 70b043a..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/000017.csv +++ /dev/null @@ -1,4 +0,0 @@ -"0","post <- extract.samples(m3.2)" -"0","plot(dat$H,dat$W,col=2,lwd=3," -"0"," xlim=c(min(dat$H),max(dat$H)), ylim=c(min(30,dat$w),max(dat$W))," -"0"," xlab=""height(cm)"", ylab=""weight(kg)"")" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/000019.csv b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/000019.csv deleted file mode 100644 index 5506a66..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/000019.csv +++ /dev/null @@ -1,2 +0,0 @@ -"0","for (j in 1:20)" -"0"," abline(a=post$a[j],b=post$b[j], lty=1)" diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/00001a.metadata b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/00001a.metadata deleted file mode 100644 index 6399179..0000000 --- a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/00001a.metadata +++ /dev/null @@ -1 +0,0 @@ -{"height":408.5290482076638,"width":661.0,"size_behavior":0,"conditions":[]} \ No newline at end of file diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/00001a.png b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/00001a.png deleted file mode 100644 index 5dc1787..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/00001a.png and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/00001a.snapshot b/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/00001a.snapshot deleted file mode 100644 index 3ec6bb3..0000000 Binary files a/.Rproj.user/shared/notebooks/39C21CC9-Statistical Thinking 2023_v2/1/s/czxt33feq8xzf/00001a.snapshot and /dev/null differ diff --git a/.Rproj.user/shared/notebooks/paths b/.Rproj.user/shared/notebooks/paths index 92c8937..2b2840f 100644 --- a/.Rproj.user/shared/notebooks/paths +++ b/.Rproj.user/shared/notebooks/paths @@ -1,3 +1,4 @@ +C:/Users/lacor/OneDrive/100. Personal/SelfDevelopment/20. Project 2019/Bayesian-Inference/MetaPolls_vFINAL.Rmd="040CBFF0" C:/Users/lacor/OneDrive/100. Personal/SelfDevelopment/20. Project 2019/Bayesian-Inference/SamplinginPractice_v7.Rmd="C44D6AD8" C:/Users/lacor/OneDrive/100. Personal/SelfDevelopment/20. Project 2019/StockMarket/Asset_Analysis_v23.Rmd="0B603800" C:/Users/lacor/OneDrive/100. Personal/SelfDevelopment/25. Project 2022/Statistical-Thinking-2023/Statistical Thinking 2023_v2.qmd="39C21CC9" diff --git a/Statistical Thinking 2023_v2.qmd b/Statistical Thinking 2023_v2.qmd index ef06545..e4a5e5c 100644 --- a/Statistical Thinking 2023_v2.qmd +++ b/Statistical Thinking 2023_v2.qmd @@ -12,6 +12,7 @@ library(rethinking) library(tidyverse) library(dplyr) library(ggplot2) +library(rstan) ``` ## Lecture 1 @@ -459,9 +460,9 @@ percs[SSlice][i] ### Workflow ```{r} - data(Howell1) -d <-Howell1[Howell1$age>=18,] +# d <-Howell1[Howell1$age>=18,] +d <-Howell1 ``` ```{r} @@ -661,3 +662,164 @@ dens(post$b[,2],lwd=3,col=4,add=TRUE) ```{r} ``` + +### Polynomials + +```{r} +plot(height~weight,d) +``` + +```{r} +d$weight_s <-(d$weight-mean(d$weight))/sd(d$weight) +d$weight_s2 <-d$weight_s^2 +m4.5 <-quap( + alist( + height ~dnorm(mu,sigma), + mu <-a+b1*weight_s+b2*weight_s2, + a ~dnorm(178,20), + b1 ~dlnorm(0,1), + b2 ~dnorm(0,1), + sigma ~dunif(0,50) + ) ,data=d) +``` + +```{r} +precis(m4.5) +``` + +```{r} +# Not working - To be Reviewed +stan_m4.5 <- " +data { + int N; + vector[N] height; + vector[N] weight_s; + vector[N] weight_s2; +} +parameters { + real a; + real b1; + real b2; + real sigma; +} + +transformed parameters { + vector[N] mu; + + for (i in 1:N) + mu[i] = a + b1*weight_s[i] + b2*weight_s2[i]; +} + +model { + // priors + a ~ normal(178,20); + b1 ~ normal(0,1); + b2 ~ normal(0,1); + sigma ~ uniform(0,50); + + // model + height ~normal(mu,sigma); +}" +data_m4.5 <- list( + N = nrow(d), + height = d$height, + weight_s = d$weight_s, + weight_s2 = d$weight_s2 +) + +#fit_m4.5 <- stan(data = data_m4.5, model_code = stan_m4.5, +# chains = 3, iter = 1000) + +``` + +```{r} +# Same Model in STAN +stan_m4.6 <- " +data { + int N; + vector[N] height; + vector[N] weight_s; + vector[N] weight_s2; +} + +transformed data{ + real bar_x1; + real x1_sd; + vector[N] x1_std; + real y_sd; + + bar_x1 = mean(weight_s); + x1_sd = sd(weight_s); + x1_std = (weight_s - bar_x1)/x1_sd; // centered and scaled + + y_sd = sd(height); +} + +parameters { + real alpha_std; + real beta1_std; + real beta3_std; + real sigma; +} + +transformed parameters { + vector[N] mu; + + for (i in 1:N) + mu[i] = alpha_std + beta1_std*weight_s[i] + + beta3_std*weight_s2[i]; +} + +model { + // priors + alpha_std ~ normal(0,20); + beta1_std ~ normal(0,2); + beta3_std ~ normal(0,2); + sigma ~ normal(5,1.5); + // sigma ~ exponential(1/y_sd); + + // model + height ~normal(mu,sigma); +} + +generated quantities { + real alpha; + real beta1; + real beta3; + + alpha = alpha_std - (beta1_std*bar_x1)/x1_sd + + (beta3_std*bar_x1^2)/x1_sd^2; + + beta1 = beta1_std/x1_sd - 2*(beta3_std*bar_x1)/x1_sd^2; + + beta3 = beta3_std/x1_sd^2; +}" + +data_m4.6 <- list( + N = nrow(d), + height = d$height, + weight_s = d$weight_s, + weight_s2 = d$weight_s2 +) + +fit_m4.6 <- stan(data = data_m4.6, + model_code = stan_m4.6, + chains = 4, + iter = 2000) + +``` + +```{r} +print(fit_m4.6, pars = c("alpha","beta1", "beta3", "sigma")) +print(fit_m4.6, pars = c("alpha_std","beta1_std", "beta3_std", "sigma")) +precis(m4.5) +``` + +```{r} +traceplot(fit_m4.6, pars = "sigma") +``` + +```{r} +sigma_sample <- extract(fit_m4.6)[["sigma"]] +hist(sigma_sample) +```