From 63d3fb78612e3b6b34ee468173c0cd5a68051efa Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Sun, 6 Sep 2020 10:51:34 +0300 Subject: [PATCH 1/2] simpler code --- Congress/congress.R | 2 +- Congress/congress.Rmd | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Congress/congress.R b/Congress/congress.R index 51c63b2c..eaaaa2ef 100644 --- a/Congress/congress.R +++ b/Congress/congress.R @@ -53,7 +53,7 @@ for (s in 1:n_sims) { } #' #### Our posterior mean and sd of how many districts the Dems will win -print(c(mean(dems_pred), sqrt(var(dems_pred))),digits=2) +print(c(mean(dems_pred), sd(dems_pred)), digits=2) #' #### Histogram of how many districts the Dems will win hist(dems_pred) diff --git a/Congress/congress.Rmd b/Congress/congress.Rmd index 0ea36d36..1229ec0d 100644 --- a/Congress/congress.Rmd +++ b/Congress/congress.Rmd @@ -78,7 +78,7 @@ for (s in 1:n_sims) { #### Our posterior mean and sd of how many districts the Dems will win ```{r } -print(c(mean(dems_pred), sqrt(var(dems_pred))),digits=2) +print(c(mean(dems_pred), sd(dems_pred)), digits=2) ``` #### Histogram of how many districts the Dems will win From 1438e7b7395098969f67bdaae2a1efc81e1848d8 Mon Sep 17 00:00:00 2001 From: Aki Vehtari Date: Sun, 6 Sep 2020 10:52:00 +0300 Subject: [PATCH 2/2] fixes thanks to Chandrasekhar Ramakrishnan --- Arsenic/data/arsenic_setup.R | 2 +- Congress/congress_plots.R | 2 +- Congress/congress_plots.Rmd | 2 +- Introclass/fakeresid.Rmd | 143 ----------------------------------- 4 files changed, 3 insertions(+), 146 deletions(-) delete mode 100644 Introclass/fakeresid.Rmd diff --git a/Arsenic/data/arsenic_setup.R b/Arsenic/data/arsenic_setup.R index 4970807b..4860b0c1 100644 --- a/Arsenic/data/arsenic_setup.R +++ b/Arsenic/data/arsenic_setup.R @@ -2,7 +2,7 @@ # Load libraries library("rprojroot") -root<-has_dirname("RAOS-Examples")$make_fix_file() +root<-has_file(".ROS-Examples-root")$make_fix_file() library("foreign") # Read in the data diff --git a/Congress/congress_plots.R b/Congress/congress_plots.R index 24cf989d..2597a341 100644 --- a/Congress/congress_plots.R +++ b/Congress/congress_plots.R @@ -24,7 +24,7 @@ savefigs <- FALSE #' #### Load packages library("rprojroot") -root<-has_dirname("RAOS-Examples")$make_fix_file() +root<-has_file(".ROS-Examples-root")$make_fix_file() #' #### Load and pre-process data congress <- vector("list", 49) diff --git a/Congress/congress_plots.Rmd b/Congress/congress_plots.Rmd index 8153e4d1..a76f1482 100644 --- a/Congress/congress_plots.Rmd +++ b/Congress/congress_plots.Rmd @@ -26,7 +26,7 @@ savefigs <- FALSE ```{r } library("rprojroot") -root<-has_dirname("RAOS-Examples")$make_fix_file() +root<-has_file(".ROS-Examples-root")$make_fix_file() ``` #### Load and pre-process data diff --git a/Introclass/fakeresid.Rmd b/Introclass/fakeresid.Rmd deleted file mode 100644 index cd5467ff..00000000 --- a/Introclass/fakeresid.Rmd +++ /dev/null @@ -1,143 +0,0 @@ ---- -title: "Regression and Other Stories: Introclass" -author: "Andrew Gelman, Jennifer Hill, Aki Vehtari" -date: "`r format(Sys.Date())`" ---- -Plot residuals vs.\ predicted values, or residuals vs.\ observed -values? See Chapter 11 in Regression and Other Stories. - -------------- - - -```{r setup, include=FALSE} -knitr::opts_chunk$set(message=FALSE, error=FALSE, warning=FALSE, comment=NA) -# switch this to TRUE to save figures in separate files -savefigs <- FALSE -``` - -**Load packages** - -```{r } -library("rprojroot") -root<-has_dirname("ROS-Examples")$make_fix_file() -library("rstanarm") -``` - -**Load data** - -```{r } -grades <- read.table(root("Introclass/data","gradesW4315.dat"), header=TRUE) -introclass <- data.frame(midterm = grades[,"Midterm"], - final = grades[,"Final"]) -``` - -**Fit linear regression model** - -The option `refresh = 0` supresses the default Stan sampling -progress output. This is useful for small data with fast -computation. For more complex models and bigger data, it can be -useful to see the progress. - -```{r } -fit_1 <- stan_glm(final ~ midterm, data = introclass, refresh = 0) -print(fit_1) -``` - -**Compute residuals**
-compute predictions from simulations - -```{r } -sims <- as.matrix(fit_1) -predicted <- colMeans(sims[,1] + sims[,2] %*% t(introclass$midterm)) -``` - -or with built-in function - -```{r } -predicted <- colMeans(posterior_linpred(fit_1)) -resid <- introclass$final - predicted -``` - -**Plot residuals vs predicted** - -```{r eval=FALSE, include=FALSE} -if (savefigs) postscript(root("Introclass/figs","fakeresid1a.ps"), height=3.8, width=4.5) -``` -```{r } -plot(predicted, resid, xlab="predicted value", ylab="residual", - main="Residuals vs.\ predicted values", mgp=c(1.5,.5,0), pch=20, yaxt="n") -axis(2, seq(-40,40,20), mgp=c(1.5,.5,0)) -abline(0, 0, col="gray", lwd=.5) -``` -```{r eval=FALSE, include=FALSE} -dev.off() -``` - -**Plot residuals vs observed** - -```{r eval=FALSE, include=FALSE} -postscript(root("Introclass/figs","fakeresid1b.ps"), height=3.8, width=4.5) -``` -```{r } -plot(introclass$final, resid, xlab="observed value", ylab="residual", main="Residuals vs.\ observed values", mgp=c(1.5,.5,0), pch=20, yaxt="n") -axis(2, seq(-40,40,20), mgp=c(1.5,.5,0)) -abline(0, 0, col="gray", lwd=.5) -``` -```{r eval=FALSE, include=FALSE} -if (savefigs) dev.off() -``` - -**Simulate fake data** - -```{r } -a <- 65 -b <- 0.7 -sigma <- 15 -n <- nrow(introclass) -introclass$final_fake <- a + b*introclass$midterm + rnorm(n, 0, 15) -fit_fake <- stan_glm(final_fake ~ midterm, data = introclass) -``` - -**Compute residuals** -compute predictions from simulations - -```{r } -sims <- as.matrix(fit_fake) -predicted_fake <- colMeans(sims[,1] + sims[,2] %*% t(introclass$midterm)) -``` - -or with built-in function - -```{r } -predicted_fake <- colMeans(posterior_linpred(fit_fake)) -resid_fake <- introclass$final_fake - predicted_fake -``` - -**Plot residuals vs predicted** - -```{r eval=FALSE, include=FALSE} -if (savefigs) postscript(root("Introclass/figs","fakeresid2a.ps"), height=3.8, width=4.5) -``` -```{r } -plot(predicted_fake, resid_fake, xlab="predicted value", ylab="residual", main="Fake data: resids vs.\ predicted", mgp=c(1.5,.5,0), pch=20, yaxt="n") -axis(2, seq(-40,40,20), mgp=c(1.5,.5,0)) -abline(0, 0, col="gray", lwd=.5) -``` -```{r eval=FALSE, include=FALSE} -if (savefigs) dev.off() -``` - -**Plot residuals vs observed** - -```{r eval=FALSE, include=FALSE} -if (savefigs) postscript(root("Introclass/figs","fakeresid2b.ps"), height=3.8, width=4.5) -``` -```{r } -plot(introclass$final_fake, resid_fake, xlab="observed value", ylab="residual", main="Fake data: resids vs.\ observed", mgp=c(1.5,.5,0), pch=20, yaxt="n") -axis(2, seq(-40,40,20), mgp=c(1.5,.5,0)) -abline(0, 0, col="gray", lwd=.5) -``` -```{r eval=FALSE, include=FALSE} -if (savefigs) dev.off() -``` -