### Install and Load Rethinking Package Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Provides instructions to install necessary R packages including 'coda', 'mvtnorm', 'devtools', and 'dagitty', and then install the 'rethinking' package from GitHub. ```R install.packages(c("coda","mvtnorm","devtools","dagitty")) library(devtools) devtools::install_github("rmcelreath/rethinking") ``` -------------------------------- ### Install and Load Rethinking Package Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Provides instructions for installing necessary R packages and the 'rethinking' package from GitHub. ```R install.packages(c("coda","mvtnorm","devtools")) library(devtools) devtools::install_github("rmcelreath/rethinking") ``` -------------------------------- ### Fitting Quap Model with Starting Values Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Fits the 'quap' model again, but this time provides starting values for 'mu' and 'sigma' based on the mean and standard deviation of the data. This can sometimes help with model convergence. ```R start <- list( mu=mean(d2$height), sigma=sd(d2$height) ) m4.1 <- quap( flist , data=d2 , start=start ) ``` -------------------------------- ### Matrix Creation Example Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt A basic example of creating a 2x2 matrix in R. ```R matrix( c(1,2,3,4) , nrow=2 , ncol=2 ) ``` -------------------------------- ### Installing cmdstanr Source: https://github.com/rmcelreath/rethinking/blob/master/README.md Instructions for installing the cmdstanr package from GitHub, which is required for within-chain multithreading with rethinking. ```R devtools::install_github("stan-dev/cmdstanr") ``` -------------------------------- ### Load Rethinking Library and Data (R) Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Loads the 'rethinking' library and the 'chimpanzees' dataset. This is a standard setup for analyses using this package. ```R library(rethinking) data(chimpanzees) d <- chimpanzees ``` -------------------------------- ### Install Rethinking 'slim' Version Source: https://github.com/rmcelreath/rethinking/blob/master/README.md Instructions for installing the 'slim' version of the Rethinking package, which excludes MCMC functionality and is suitable for working through the initial chapters of the associated textbook. ```R install.packages(c("coda","mvtnorm","devtools","loo","dagitty")) devtools::install_github("rmcelreath/rethinking@slim") ``` -------------------------------- ### Install Rethinking Package Source: https://github.com/rmcelreath/rethinking/blob/master/README.md Instructions for installing the Rethinking R package from GitHub, including necessary dependencies and the cmdstanr package for MCMC functionality. ```R install.packages(c("coda","mvtnorm","devtools","loo","dagitty","shape")) devtools::install_github("rmcelreath/rethinking") ``` -------------------------------- ### Running Stan Model with Imputation Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Prepares data and starting values for the Stan model with neocortex imputation and runs the model using the 'rstan' package. This involves setting up the data list, initial parameters, and calling the stan function. ```R data_list <- list( N = nrow(d), kcal = d$kcal.per.g, neocortex = nc, logmass = d$logmass, nc_missing = nc_missing, nc_num_missing = max(nc_missing) ) start <- list( alpha=mean(d$kcal.per.g), sigma=sd(d$kcal.per.g), bN=0, bM=0, mu_nc=0.68, sigma_nc=0.06, nc_impute=rep( 0.5 , max(nc_missing) ) ) library(rstan) m14.3stan <- stan( model_code=model_code , data=data_list , init=list(start) , iter=1e4 , chains=1 ) ``` -------------------------------- ### R: Basic Plot Initialization Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Initializes a plot with specified axis limits, labels, and tick mark configurations. This is often used as a starting point for more complex visualizations. ```R plot( 1 , 1 , xlab="intention" , ylab="probability" , xlim=c(0,1) , ylim=c(0,1) , xaxp=c(0,1,1) , yaxp=c(0,1,2) ) ``` -------------------------------- ### R: Loading Example Data Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Loads the 'KosterLeckie' dataset, which contains dyadic data likely related to social interactions or resource exchange. This data is prepared for hierarchical modeling. ```R library(rethinking) data(KosterLeckie) ``` -------------------------------- ### Loading and Preparing Kline Data Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Loads the Kline dataset, which contains information on US historical births, and prepares it for analysis. ```R library(rethinking) data(Kline) d <- Kline d ``` -------------------------------- ### Loading Rethinking Library and Data (R) Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Loads the 'rethinking' library and the 'chimpanzees' dataset for analysis. ```R library(rethinking) data(chimpanzees) d <- chimpanzees ``` -------------------------------- ### Model Summary and Comparison Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Generates summaries of model parameters and compares different models. ```R precis(m12.3,depth=2) precis(m12.5,depth=2) # depth=2 displays varying effects compare(m12.4,m12.5) ``` -------------------------------- ### Probability Distribution Calculations Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Demonstrates calculating probabilities and proportions using binomial distributions and normalizing vectors. ```R ways <- c( 0 , 3 , 8 , 9 , 0 ) ways/sum(ways) ``` ```R dbinom( 6 , size=9 , prob=0.5 ) ``` -------------------------------- ### Loading Rethinking Data Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Loads the 'homeworkch3' dataset from the 'rethinking' library. This is a common step for using provided datasets in the library. ```R library(rethinking) data(homeworkch3) ``` -------------------------------- ### Number of Cafes Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Sets the number of cafes for the simulation. ```R N_cafes <- 20 ``` -------------------------------- ### Load UCBadmit Data Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Loads the UCBadmit dataset from the 'rethinking' package for analysis. ```R library(rethinking) data(UCBadmit) d <- UCBadmit ``` -------------------------------- ### Defining Probability Distributions (R) Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Creates a list of probability distributions represented as vectors of probabilities for four categories. ```R p <- list() p$A <- c(0,0,10,0,0) p$B <- c(0,1,8,1,0) p$C <- c(0,2,6,2,0) p$D <- c(1,2,4,2,1) p$E <- c(2,2,2,2,2) ``` -------------------------------- ### Data Loading and Preparation Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Loads the `cherry_blossoms` dataset, selects complete cases for the `doy` variable, and generates a list of quantiles for spline knot placement. ```R library(rethinking) data(cherry_blossoms) d <- cherry_blossoms precis(d) d2 <- d[ complete.cases(d$doy) , ] # complete cases on doy num_knots <- 15 knot_list <- quantile( d2$year , probs=seq(0,1,length.out=num_knots) ) ``` -------------------------------- ### Reproducible MCMC Sampling Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Shows how to generate reproducible samples from a posterior distribution using `set.seed` before calling the sampling function. ```R p_grid <- seq( from=0 , to=1 , length.out=1000 ) prior <- rep( 1 , 1000 ) likelihood <- dbinom( 6 , size=9 , prob=p_grid ) posterior <- likelihood * prior posterior <- posterior / sum(posterior) set.seed(100) samples <- sample( p_grid , prob=posterior , size=1e4 , replace=TRUE ) ``` -------------------------------- ### R Data Loading for Panda Nuts Dataset Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Loads the 'Panda_nuts' dataset from the 'rethinking' library. ```R library(rethinking) data(Panda_nuts) ``` -------------------------------- ### Loading Data and Package Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Loads the 'rethinking' package and the 'milk' dataset, which contains information about milk composition. This is a prerequisite for subsequent analysis. ```R library(rethinking) data(milk) d <- milk ``` -------------------------------- ### Simulating and Analyzing Log-Transformed Normal Variables Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Simulates values from a normal distribution, exponentiates them to get log-normal values, and calculates the mean of the simulated log-normal data. ```R a <- rnorm(1e4,0,10) lambda <- exp(a) mean( lambda ) ``` -------------------------------- ### Compare Model Coefficients Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Compares the coefficients of multiple fitted models using 'coeftab'. Requires the 'rethinking' package. ```R library(rethinking) coeftab(m11.1,m11.2,m11.3) ``` -------------------------------- ### Data Simulation and Preparation Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Simulates data for pond populations, including true effects and observed outcomes, and prepares it for modeling. ```R dsim <- data.frame( pond=1:nponds , ni=ni , true_a=a_pond ) dsim$si <- rbinom( nponds , prob=logistic(dsim$true_a) , size=dsim$ni ) dsim$p_nopool <- dsim$si / dsim$ni a <- 1.4 sigma <- 1.5 nponds <- 60 ni <- as.integer( rep( c(5,10,25,35) , each=15 ) ) a_pond <- rnorm( nponds , mean=a , sd=sigma ) dsim <- data.frame( pond=1:nponds , ni=ni , true_a=a_pond ) dsim$si <- rbinom( nponds,prob=logistic( dsim$true_a ),size=dsim$ni ) dsim$p_nopool <- dsim$si / dsim$ni ``` -------------------------------- ### R: Setting Up Simulation Parameters Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Sets up parameters for a simulation study, including the overall intercept (`a`), standard deviation (`sigma`), number of ponds (`nponds`), and the number of individuals per pond (`ni`). ```R a <- 1.4 sigma <- 1.5 nponds <- 60 ni <- as.integer( rep( c(5,10,25,35) , each=15 ) ) ``` -------------------------------- ### R: Simulated Data for Causal Inference Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Simulates data for causal inference analysis, including a directed acyclic graph (DAG) representation. This setup is used to explore instrumental variables. ```R set.seed(73) N <- 500 U_sim <- rnorm( N ) Q_sim <- sample( 1:4 , size=N , replace=TRUE ) E_sim <- rnorm( N , U_sim + Q_sim ) W_sim <- rnorm( N , -U_sim + 0.2*E_sim ) dat_sim <- list( W=standardize(W_sim) , E=standardize(E_sim) , Q=standardize(Q_sim) ) ``` -------------------------------- ### R: Loading Reedfrog Data Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Loads the `reedfrogs` dataset from the `rethinking` library and displays its structure. ```R library(rethinking) data(reedfrogs) d <- reedfrogs str(d) ``` -------------------------------- ### Predictive Modeling and Visualization (Rethinking) Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Generates predictions from a fitted model and visualizes them alongside the data. This involves using the 'link' function to get posterior predictions and plotting them with confidence intervals. ```R # make a plot window with three panels in a single row par(mfrow=c(1,3)) # 1 row, 3 columns # loop over values of water.c and plot predictions shade.seq <- -1:1 for ( w in -1:1 ) { dt <- d[d$water.c==w,] plot( blooms ~ shade.c , data=dt , col=rangi2 , main=paste("water.c =",w) , xaxp=c(-1,1,2) , ylim=c(0,362) , xlab="shade (centered)" ) mu <- link( m7.9 , data=data.frame(water.c=w,shade.c=shade.seq) ) mu.mean <- apply( mu , 2 , mean ) mu.PI <- apply( mu , 2 , PI , prob=0.97 ) lines( shade.seq , mu.mean ) lines( shade.seq , mu.PI[1,] , lty=2 ) lines( shade.seq , mu.PI[2,] , lty=2 ) } ``` -------------------------------- ### Posterior Calculation with Grid Approximation Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Expands on grid approximation by calculating the joint posterior distribution of 'mu' and 'sigma'. It computes the log-likelihood, combines it with priors, and normalizes to get the posterior probability. ```R mu.list <- seq( from=140, to=160 , length.out=200 ) sigma.list <- seq( from=4 , to=9 , length.out=200 ) post <- expand.grid( mu=mu.list , sigma=sigma.list ) post$LL <- sapply( 1:nrow(post) , function(i) sum( dnorm( d2$height , mean=post$mu[i] , sd=post$sigma[i] , log=TRUE ) ) ) post$prod <- post$LL + dnorm( post$mu , 178 , 20 , TRUE ) + dunif( post$sigma , 0 , 50 , TRUE ) post$prob <- exp( post$prod - max(post$prod) ) ``` -------------------------------- ### Reproducible Sampling with Seed Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Shows how to set a random seed in R to ensure reproducibility of random sampling from a posterior distribution. ```R p_grid <- seq( from=0 , to=1 , length.out=1000 ) prior <- rep( 1 , 1000 ) likelihood <- dbinom( 6 , size=9 , prob=p_grid ) posterior <- likelihood * prior posterior <- posterior / sum(posterior) set.seed(100) samples <- sample( p_grid , prob=posterior , size=1e4 , replace=TRUE ) ``` -------------------------------- ### R Code for Multinomial Regression Setup and Analysis Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt This R code sets up the data and runs a Stan model for multinomial regression. It then uses the `precis` function to summarize the posterior distributions of the model parameters. ```R b <- c(-2,0,2) career <- rep(NA,N) # empty vector of choices for each individual for ( i in 1:N ) { score <- 0.5*(1:3) + b*family_income[i] p <- softmax(score[1],score[2],score[3]) career[i] <- sample( 1:3 , size=1 , prob=p ) } dat_list <- list( N=N , K=3 , career=career , family_income=family_income ) m11.14 <- stan( model_code=code_m11.14 , data=dat_list , chains=4 ) precis( m11.14 , 2 ) ``` -------------------------------- ### Preparing Data for Poisson Models Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Creates a data list for fitting Poisson regression models to the Kline dataset, including total tools, scaled log population, and contact ID. ```R dat <- list( T = d$total_tools , P = d$P , cid = d$contact_id ) # intercept only m11.9 <- ulam( alist( T ~ dpois( lambda ), log(lambda) <- a, a ~ dnorm( 3 , 0.5 ) ), data=dat , chains=4 , log_lik=TRUE ) # interaction model m11.10 <- ulam( alist( T ~ dpois( lambda ), log(lambda) <- a[cid] + b[cid]*P, a[cid] ~ dnorm( 3 , 0.5 ), b[cid] ~ dnorm( 0 , 0.2 ) ), data=dat , chains=4 , log_lik=TRUE ) ``` -------------------------------- ### Loading and Inspecting a Dataset (R) Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt This code loads the 'Moralizing_gods' dataset and then uses `str` to display its structure, including variable names, types, and a preview of the data. This is a standard first step when working with a new dataset. ```R data(Moralizing_gods) str(Moralizing_gods) ``` -------------------------------- ### R: Extracting and Processing Posterior Samples Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Extracts posterior samples from a fitted model and calculates expected values for group-specific effects. This involves exponentiating the log-transformed parameters to get back to the original scale. ```R post <- extract.samples( m14.7 ) g <- sapply( 1:25 , function(i) post$a + post$gr[,i,1] ) r <- sapply( 1:25 , function(i) post$a + post$gr[,i,2] ) Eg_mu <- apply( exp(g) , 2 , mean ) Er_mu <- apply( exp(r) , 2 , mean ) ``` -------------------------------- ### Plotting Phylogenetic Trees with Ape Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt This snippet demonstrates how to plot a phylogenetic tree using the 'ape' package in R. It includes installing the package if necessary and applying a fan layout with specific labeling and scaling options. ```R library(ape) plot( ladderize(Primates301_nex) , type="fan" , font=1 , no.margin=TRUE , label.offset=1 , cex=0.5 ) ``` -------------------------------- ### Counterfactual Divorce Prediction (Marriage) Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Predicts divorce outcomes based on a counterfactual scenario where Marriage.s is varied while MedianAgeMarriage.s is held constant at its average. It uses `link` and `sim` to get mean predictions and uncertainty intervals. ```R # prepare new counterfactual data A.avg <- mean( d$MedianAgeMarriage.s ) R.seq <- seq( from=-3 , to=3 , length.out=30 ) pred.data <- data.frame( Marriage.s=R.seq, MedianAgeMarriage.s=A.avg ) # compute counterfactual mean divorce (mu) mu <- link( m5.3 , data=pred.data ) mu.mean <- apply( mu , 2 , mean ) mu.PI <- apply( mu , 2 , PI ) # simulate counterfactual divorce outcomes R.sim <- sim( m5.3 , data=pred.data , n=1e4 ) R.PI <- apply( R.sim , 2 , PI ) # display predictions, hiding raw data with type="n" plot( Divorce ~ Marriage.s , data=d , type="n" ) mtext( "MedianAgeMarriage.s = 0" ) lines( R.seq , mu.mean ) shade( mu.PI , R.seq ) shade( R.PI , R.seq ) ``` -------------------------------- ### Setting Up Plot for Probability Visualization in R Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Initializes an empty plot with specified axis labels, limits, and tick mark configurations, preparing it for plotting probability distributions. ```R plot( NULL , type="n" , xlab="intention" , ylab="probability" , xlim=c(0,1) , ylim=c(0,1) , xaxp=c(0,1,1) , yaxp=c(0,1,2) ) ``` -------------------------------- ### Extracting Entropies and Distributions (R) Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Extracts the calculated entropies and the corresponding probability distributions from the results of the 'sim.p' simulation. ```R entropies <- as.numeric(H[1,]) distributions <- H[2,] ``` -------------------------------- ### Simulating Binomial and Normal Data (R) Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt This snippet simulates data for a model. It generates a standard normal variable S, and then a binomial variable H based on the inverse logit of S. This is a common setup for logistic regression-like models. ```R N <- 100 S <- rnorm( N ) H <- rbinom( N , size=10 , inv_logit(S) ) ``` -------------------------------- ### Compare Model Performance Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Compares the performance of multiple fitted models using 'compare'. Requires the 'rethinking' package. ```R library(rethinking) compare( m11.1 , m11.2 , m11.3 , refresh=0.1 ) ``` -------------------------------- ### Loading and Preparing UCBadmit Data Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Loads the UCBadmit dataset and prepares it for modeling by creating a list with admissions, applications, and gender indicator. ```R library(rethinking) data(UCBadmit) d <- UCBadmit dat_list <- list( admit = d$admit, applications = d$applications, gid = ifelse( d$applicant.gender=="male" , 1 , 2 ) ) ``` -------------------------------- ### Data Cleaning and Model Fitting with map2stan in R Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt This snippet cleans the dataset by removing a column and then re-uses a map fit to get the formula for a new model. It fits the model using 'map2stan' and then summarizes the results using 'precis'. ```R # clean NAs from the data d2 <- d d2$recipient <- NULL # re-use map fit to get the formula m10.3stan <- map2stan( m10.3 , data=d2 , iter=1e4 , warmup=1000 ) precis(m10.3stan) ``` -------------------------------- ### Loading and Inspecting Data with Rethinking Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Shows how to load the 'Howell1' dataset from the 'rethinking' library and inspect its structure and contents. This is a common first step in data analysis. ```R library(rethinking) data(Howell1) d <- Howell1 ``` ```R str( d ) ``` ```R d$height ``` ```R d2 <- d[ d$age >= 18 , ] ``` -------------------------------- ### Ordered Logistic Model with `quap` in R Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Fits an ordered logistic regression model using `quap` (quadratic approximation posterior). This model specifies multiple cutpoints (`a1` to `a6`) with normal priors and provides starting values for the parameters. ```R m12.4q <- quap( alist( response ~ dordlogit( 0 , c(a1,a2,a3,a4,a5,a6) ), c(a1,a2,a3,a4,a5,a6) ~ dnorm( 0 , 1.5 ) ) , data=d , start=list(a1=-2,a2=-1,a3=0,a4=1,a5=2,a6=2.5) ) ``` -------------------------------- ### Model Summary and Diagnostics with `ulam` Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Displays a summary of the fitted `ulam` model using `precis` and shows the model structure with `show`. It also demonstrates using `precis` with a depth argument and plotting pairs and traceplots for diagnostics. ```R precis( m9.1 , depth=2 ) show( m9.1 ) precis( m9.1 , 2 ) pairs( m9.1 ) traceplot( m9.1 ) trankplot( m9.1 ) ``` -------------------------------- ### Simulating Data for Causal Model (M -> K <- N, N -> M) (R) Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Simulates data for a causal model where M influences K and N, and N also influences M. This setup is common in observational studies where unobserved confounding might exist. ```R # M -> K <- N # N -> M n <- 100 N <- rnorm( n ) M <- rnorm( n , N ) K <- rnorm( n , N - M ) d_sim2 <- data.frame(K=K,N=N,M=M) ``` -------------------------------- ### Exploring Priors with `ulam` Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Demonstrates fitting a simple model with `ulam` to explore the impact of different priors on parameters `a` and `b`, using Gaussian and Cauchy distributions. ```R mp <- ulam( alist( a ~ dnorm(0,1), b ~ dcauchy(0,1) ), data=list(y=1) , chains=1 ) ``` -------------------------------- ### Simulating Binomial Random Variables Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Demonstrates how to simulate random variables from a binomial distribution using `dbinom` and `rbinom`. It also shows how to visualize the distribution of simulated data using `simplehist`. ```R dbinom( 0:2 , size=2 , prob=0.7 ) ``` ```R rbinom( 1 , size=2 , prob=0.7 ) ``` ```R rbinom( 10 , size=2 , prob=0.7 ) ``` ```R dummy_w <- rbinom( 1e5 , size=2 , prob=0.7 ) table(dummy_w)/1e5 ``` ```R dummy_w <- rbinom( 1e5 , size=9 , prob=0.7 ) simplehist( dummy_w , xlab="dummy water count" ) ``` ```R w <- rbinom( 1e4 , size=9 , prob=0.6 ) ``` ```R w <- rbinom( 1e4 , size=9 , prob=samples ) ``` -------------------------------- ### Generate Data for Posterior Prediction Plot (R) Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Creates a new data list `dat` to generate posterior predictions for all actor-treatment combinations. It then uses the `link` function to get predictions from the `m11.4` model and calculates the mean and credible intervals for these predictions. ```R dat <- list( actor=rep(1:7,each=4) , treatment=rep(1:4,times=7) ) p_post <- link( m11.4 , data=dat ) p_mu <- apply( p_post , 2 , mean ) p_ci <- apply( p_post , 2 , PI ) ``` -------------------------------- ### Milk Composition Data Preparation Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Loads the 'milk' dataset and prepares it for modeling by calculating the proportion of neocortex and the log of body mass. Requires the 'rethinking' library. ```R library(rethinking) data(milk) d <- milk d$neocortex.prop <- d$neocortex.perc / 100 d$logmass <- log(d$mass) ``` -------------------------------- ### Loading and Inspecting Milk Dataset Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Loads the 'milk' dataset from the 'rethinking' package and displays its structure, showing the variables and their data types. ```R library(rethinking) data(milk) d <- milk str(d) ``` -------------------------------- ### Simulating Product Growth Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Calculates the product of 12 numbers, each being 1 plus a random number from a uniform distribution between 0 and 0.1. This simulates a multiplicative growth process. ```R prod( 1 + runif(12,0,0.1) ) ``` -------------------------------- ### Visualizing Intercept and Slope Variation (Reduced Slope) Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Similar to the previous example, but with a smaller standard deviation for the slope, resulting in less variation in the lines. ```R set.seed(10) N <- 100 a <- rnorm( N , 3 , 0.5 ) b <- rnorm( N , 0 , 0.2 ) plot( NULL , xlim=c(-2,2) , ylim=c(0,100) ) for ( i in 1:N ) curve( exp( a[i] + b[i]*x ) , add=TRUE , col=grau() ) ``` -------------------------------- ### Simulating Birth Data Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Generates two vectors, birth1 and birth2, containing binary data, likely representing birth outcomes. This is a basic data simulation example. ```R birth1 <- c(1,0,0,0,1,1,0,1,0,1,0,0,1,1,0,1,1,0,0,0,1,0,0,0,1,0, 0,0,0,1,1,1,0,1,0,1,1,1,0,1,0,1,1,0,1,0,0,1,1,0,1,0,0,0,0,0,0,0, 1,1,0,1,0,0,1,0,0,0,1,0,0,1,1,1,1,0,1,0,1,1,1,1,1,0,0,1,0,1,1,0, 1,0,1,1,1,0,1,1,1,1) birth2 <- c(0,1,0,1,0,1,1,1,0,0,1,1,1,1,1,0,0,1,1,1,0,0,1,1,1,0, 1,1,1,0,1,1,1,0,1,0,0,1,1,1,1,0,0,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,0,1,1,0,1,1,0,1,1,1,0,0,0,0,0,0,1,0,0,0,1,1,0,0,1,0,0,1,1, 0,0,0,1,1,1,0,0,0,0) ``` -------------------------------- ### Bayesian Model Fitting with Different Priors Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Demonstrates fitting Bayesian models with `ulam` using different prior specifications for the intercept and sigma parameters, comparing results using `precis`. ```R y <- c(-1,1) set.seed(11) m9.2 <- ulam( alist( y ~ dnorm( mu , sigma ) , mu <- alpha , alpha ~ dnorm( 0 , 1000 ) , sigma ~ dexp( 0.0001 ) ) , data=list(y=y) , chains=3 ) precis( m9.2 ) set.seed(11) m9.3 <- ulam( alist( y ~ dnorm( mu , sigma ) , mu <- alpha , alpha ~ dnorm( 1 , 10 ) , sigma ~ dexp( 1 ) ) , data=list(y=y) , chains=3 ) precis( m9.3 ) ``` -------------------------------- ### Data Loading and Preparation (Rethinking) Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Loads the 'rugged' dataset from the 'rethinking' package, performs log transformation on GDP per capita, and cleans the data by removing rows with missing values. ```R library(rethinking) data(rugged) d$log_gdp <- log(d$rgdppc_2000) dd <- d[ complete.cases(d$rgdppc_2000) , ] ``` -------------------------------- ### Simulating Binomial Random Variables Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Demonstrates how to simulate random variables from a binomial distribution using `dbinom` and `rbinom`, and visualizes the distribution with a histogram. ```R dbinom( 0:2 , size=2 , prob=0.7 ) ``` ```R rbinom( 1 , size=2 , prob=0.7 ) ``` ```R rbinom( 10 , size=2 , prob=0.7 ) ``` ```R dummy_w <- rbinom( 1e5 , size=2 , prob=0.7 ) table(dummy_w)/1e5 ``` ```R dummy_w <- rbinom( 1e5 , size=9 , prob=0.7 ) simplehist( dummy_w , xlab="dummy water count" ) ``` ```R w <- rbinom( 1e4 , size=9 , prob=0.6 ) ``` ```R w <- rbinom( 1e4 , size=9 , prob=samples ) ``` -------------------------------- ### Loading and Inspecting Howell1 Dataset (R) Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Loads the `Howell1` dataset from the `rethinking` package and inspects its structure using `str`. This dataset contains height and weight information. ```R library(rethinking) data(Howell1) d <- Howell1 str(d) ``` -------------------------------- ### Glimmer Function for Binomial GLM Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Applies the `glimmer` function, likely a wrapper or alternative for fitting generalized linear models, to the same chimpanzee dataset and model specification as the `glm` example. ```R glimmer( pulled_left ~ prosoc_left * condition - condition , data=chimpanzees , family=binomial ) ``` -------------------------------- ### Loading and Preparing Howell1 Dataset Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Loads the 'Howell1' dataset from the 'rethinking' library and creates a subset 'd2' containing only individuals aged 18 and above. ```R library(rethinking) data(Howell1) d <- Howell1 d2 <- d[ d$age >= 18 , ] ``` -------------------------------- ### Visualizing Probability Distributions Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Illustrates how to plot probability density functions using 'curve'. It shows examples for a normal distribution and a uniform distribution, which are often used as priors in Bayesian modeling. ```R curve( dnorm( x , 178 , 20 ) , from=100 , to=250 ) ``` ```R curve( dunif( x , 0 , 50 ) , from=-10 , to=60 ) ``` -------------------------------- ### Calculate Entropy of a Distribution (R) Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Calculates the entropy of a given probability distribution using the formula -sum(p*log(p)). This is a fundamental concept in information theory and is used in several examples. ```R H <- sapply( p_norm , function(q) -sum(ifelse(q==0,0,q*log(q))) ) ``` ```R sapply( p , function(p) -sum( p*log(p) ) ) ``` -------------------------------- ### Fitting a Bayesian Model with `ulam` (Single Chain) Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Fits a Bayesian model using the `ulam` function with a single chain. The model structure is similar to the `quap` example, defining likelihood and priors. ```R m9.1 <- ulam( alist( log_gdp_std ~ dnorm( mu , sigma ) , mu <- a[cid] + b[cid]*( rugged_std - 0.215 ) , a[cid] ~ dnorm( 1 , 0.1 ) , b[cid] ~ dnorm( 0 , 0.3 ) , sigma ~ dexp( 1 ) ) , data=dat_slim , chains=1 ) ``` -------------------------------- ### Preparing Data for `ulam` Model Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Creates a list containing the standardized log GDP, standardized ruggedness, and continent ID, formatted for use with the `ulam` function. ```R dat_slim <- list( log_gdp_std = dd$log_gdp_std, rugged_std = dd$rugged_std, cid = as.integer( dd$cid ) ) str(dat_slim) ``` -------------------------------- ### Recalculating Posterior with Subset Data Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Calculates the posterior distribution for 'mu' and 'sigma' using a smaller subset of height data ('d3'). It then samples from this posterior and plots the results, similar to previous examples. ```R mu.list <- seq( from=150, to=170 , length.out=200 ) sigma.list <- seq( from=4 , to=20 , length.out=200 ) post2 <- expand.grid( mu=mu.list , sigma=sigma.list ) post2$LL <- sapply( 1:nrow(post2) , function(i) sum( dnorm( d3 , mean=post2$mu[i] , sd=post2$sigma[i] , log=TRUE ) ) ) post2$prod <- post2$LL + dnorm( post2$mu , 178 , 20 , TRUE ) + dunif( post2$sigma , 0 , 50 , TRUE ) post2$prob <- exp( post2$prod - max(post2$prod) ) sample2.rows <- sample( 1:nrow(post2) , size=1e4 , replace=TRUE , prob=post2$prob ) sample2.mu <- post2$mu[ sample2.rows ] sample2.sigma <- post2$sigma[ sample2.rows ] plot( sample2.mu , sample2.sigma , cex=0.5 , col=col.alpha(rangi2,0.1) , xlab="mu" , ylab="sigma" , pch=16 ) ``` -------------------------------- ### Simulating Entropy and Distributions (R) Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Defines a function 'sim.p' that simulates probabilities for a four-outcome event given a parameter 'G', calculates their entropy, and returns both. It then uses this function to generate 100,000 samples. ```R sim.p <- function(G=1.4) { x123 <- runif(3) x4 <- ( (G)*sum(x123)-x123[2]-x123[3] )/(2-G) z <- sum( c(x123,x4) ) p <- c( x123 , x4 )/z list( H=-sum( p*log(p) ) , p=p ) } H <- replicate( 1e5 , sim.p(1.4) ) dens( as.numeric(H[1,]) , adj=0.1 ) ``` -------------------------------- ### Calculating WAIC Difference and Standard Error (Another Pair) Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Similar to a previous example, this calculates the difference in WAIC between models `m6.6` and `m6.8` and estimates the standard error of this difference, aiding in model comparison. ```R set.seed(92) waic_m6.6 <- WAIC( m6.6 , pointwise=TRUE )$WAIC diff_m6.6_m6.8 <- waic_m6.6 - waic_m6.8 sqrt( n*var( diff_m6.6_m6.8 ) ) ``` -------------------------------- ### Linear Model Specification (R) Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes_ed1.txt Shows different ways to specify linear models using the 'lm' function in R, including interactions and variable transformations. These examples illustrate common formula syntax for regression. ```R m7.x <- lm( y ~ x + z + x*z , data=d ) ``` ```R m7.x <- lm( y ~ x*z , data=d ) ``` ```R m7.x <- lm( y ~ x + x*z - z , data=d ) ``` ```R m7.x <- lm( y ~ x*z*w , data=d ) ``` -------------------------------- ### Simulating Log-Transformed Growth Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Simulates a growth process similar to 'big' but takes the natural logarithm of the product. This is repeated 10,000 times. ```R log.big <- replicate( 10000 , log(prod(1 + runif(12,0,0.5)))) ``` -------------------------------- ### Simulating Prior Height with Wider Mu Prior Source: https://github.com/rmcelreath/rethinking/blob/master/book_code_boxes.txt Simulates a prior distribution for height, similar to the previous example, but with a wider prior for 'mu' (mean 178, sd 100). It then displays the density of the simulated prior heights. ```R sample_mu <- rnorm( 1e4 , 178 , 100 ) prior_h <- rnorm( 1e4 , sample_mu , sample_sigma ) dens( prior_h ) ```