R Code rethinking statistics ch4

0.0(0)
Studied by 0 people
call kaiCall Kai
Locked
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/26

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 7:39 PM on 8/29/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

27 Terms

1
New cards

library(rethinking)

load the rethinking library in

2
New cards

data(Howell)

load data howell from a library

3
New cards

d ← Howell

put Howell data into a variable

4
New cards

str(d)

inspect structure of data d

5
New cards

d$height

extract data in data frame d for height

6
New cards

d2 <- d[d$age >= 18,]

extract data from data frame d where the age is greater than or equal to 18 and assign to variable d2

7
New cards

curve(dnorm( x, 178, 20), from=100, to=250)

make a prior for a normal distribution of x with mean 178 and 95% CI of 80, plot from 100 to 250

8
New cards

dunif(x, 0, 50)

make a prior with a uniform distribution from 0 to 50

9
New cards

sample <- rnorm(1e4, 178, 20)

take 10,000 samples from a normal distribution with mean 178 and standard deviation 20 and assign them to a sample variable

10
New cards

sample ← runif(1e4, 0, 50)

take 10,000 samples from a uniform distibution from 0 to 50

11
New cards

sum(sample) / 1e4

take the average of a sample variable with 10,000 entries

12
New cards

prior_h <- rnorm(1e4, sample_mu, sample_sigma)

sample 10,000 values from a set of sample_mu and sample_sigma

13
New cards

dens(sample)

create a density plot of sample values

14
New cards

sample a row then take samples from that row

how to sample a posterior that contains multiple values

15
New cards

alist (
height ~ dnorm(mu , sigma),

mu ~ dnorm(178 , 20),

sigma ~ dunif(0 , 50)

)

how to create a list with variable relations, height follows a normal distribution, mu follows normal distribution 178 average, with stdev 20, sigma follows uniform from 0 to 50

16
New cards

m4.1 <- map (flist , data=d2)

Create a maximum a posteri model from d2 onto a quadratic function using the variables described in flist and their

17
New cards

precis(m4.1)

find the mean and stdev, and 89% confidence interval of m4.1

18
New cards

vcov(m4.1)

how to find the covariance of MAP model m4.1

19
New cards

cov2cor(vcov(m4.1))

how to find the relationship between mu and sigma of MAP m4.1

20
New cards

extract.samples(m4.1, n=1e4)

how to sample 10,000 times multi-dimensional posterioir from a MAP value m4.1, output is a data frame

21
New cards

rm(list = ls())

how to clear environment in R

22
New cards

plot(d2$height ~ d2$weight)

plot height (y) over weight (x)

23
New cards

precis(m4.3, corr=TRUE)

find a precis model with correlation matrix for map m4.3

24
New cards

rethinking link function

take all values of x for the model, sample from the posterior

25
New cards

str(x)

how to find the structure (N rows and cols) for x

26
New cards

link functuon

creates a sample of dist values for 1000 values of mu

27
New cards

standardizing data

puts the mean at 0 and makes the points clump around it better, done by subtracting everything by the mean then dividing by a standard deviation