This lecture introduced categorical predictors and a method to estimate curvilinear relationships. We also learned how to develop models scientifically using DAGS. Data by itself does not provide any causal information. Causal inference requires additional assumptions. Simple directed acyclic graph (DAG) models of causation are one way to represent those assumptions.
Place each answer inside the code chunk (grey box). The code chunks should contain a text response or a code that completes/answers the question or activity requested. Make sure to include plots if the question requests them.
Finally, upon completion, name your final output .html
file as: YourName_ANLY505-Year-Semester.html and submit the
file to Canvas. Each question is worth 5 points.
3-1. From the Howell1 data set, consider only the people younger than 13 years old. Now suppose the causal association between age and weight might be different for boys and girls. Use a singular regression, with a categorical variable for sex, to estimate the total causal effect of age on weight separately for boys and girls. How do boys and girls differ? Provide one or more posterior contrasts as a summary.
library(rethinking)
data(Howell1)
d <- Howell1
d <- d[ d$age < 13 , ]
dat <- list(W=d$weight,A=d$age,S=d$male+1)
m3 <- quap(
alist(
W ~ dnorm( mu , sigma ),
mu <- a[S] + b[S]*A,
a[S] ~ dnorm(5,1),
b[S] ~ dlnorm(0,1),
sigma ~ dexp(1)
), data=dat )
# blank(bty="n")
plot( d$age , d$weight , lwd=3, col=ifelse(d$male==1,4,2) , xlab="age (years)" , ylab="weight (kg)" )
Aseq <- 0:12
# girls
muF <- link(m3,data=list(A=Aseq,S=rep(1,13)))
shade( apply(muF,2,PI,0.99) , Aseq , col=col.alpha(2,0.5) )
lines( Aseq , apply(muF,2,mean) , lwd=3 , col=2 )
# boys
muM <- link(m3,data=list(A=Aseq,S=rep(2,13)))
shade( apply(muM,2,PI,0.99) , Aseq , col=col.alpha(4,0.5) )
lines( Aseq , apply(muM,2,mean) , lwd=3 , col=4 )
3-2. Select out all the rows in the Howell1 data
with ages below 18 years of age. If you do it right, you should end up
with a new data frame with 192 rows in it. Fit a linear regression to
these data, using quap. Present and interpret the
estimates. For every 10 units of increase in weight, how much taller
does the model predict a child gets?
d2 <- Howell1[Howell1$age < 18, ]
nrow(d2)
## [1] 192
mod1 <- quap ( alist (
weight ~ dnorm(m,s),
m <- a + b * age,
a ~ dlnorm(1,10),
b ~ dlnorm(0,1),
s ~ exp(1)
), data = d2)
precis(mod1)
## mean sd 5.5% 94.5%
## a 6.60365 0.34486623 6.052487 7.154813
## b 1.52873 0.03669107 1.470091 1.587370
# The estimate of a indicates that around 6.6 cm is an estimated height for a participant below 18 years old with a weight of 0 kg. The estimate of b indicates that, in this sample, we can expect an increase in height of around 1.5 cm for each additional unit of weight.
3-3. In Chapter 4, in the book, we used 15 knots with the cherry blossom spline. Increase the number of knots and observe what happens to the resulting spline. Then adjust the width of the prior on the weights (i.e. change the standard deviation of the prior) and watch what happens. What do you think the combination of knot number and the prior on the weights controls?
data("cherry_blossoms")
# remove na's
cherry_blossoms <- cherry_blossoms %>%
as_tibble() %>%
drop_na()
# define nr of knots
library(splines)
# First, we will make function for the dependant on number of knots and the prior on weights
cherry_spliner <- function(nr_knots, vl_sigma){
# Second, we will get knot points
knot_list <- cherry_blossoms$year %>%
quantile(probs = seq(0, 1, length.out = nr_knots)) %>%
discard(~ . %in% c(851, 1980))
# Third, we will construct basis functions
B <- cherry_blossoms$year %>% bs(knots = knot_list, degree = 3, intercept = TRUE)
# Fourth, we will run bspline regression
m4.7 <- alist(D ~ dnorm(mu, sigma),
mu <- a + B %*% w,
a ~ dnorm(100, 10),
w ~ dnorm(0, my_sigma),
sigma ~ dexp(1)) %>%
quap(data = list(D = cherry_blossoms$doy, B = B, my_sigma = vl_sigma),
start = list(w = rep(0, ncol(B))))
# In order get 97% posterior interval for mean
post_int <- m4.7 %>%
link() %>% as_tibble() %>%
map_dfr(PI, prob = 0.97) %>%
select("lower_pi" = '2%', "upper_pi" = '98%') %>%
add_column(year = cherry_blossoms$year) %>%
# add doy
left_join(cherry_blossoms, by = "year")
# we will then plot it and add nr_knots and vl_sigma to plot title
ggplot(post_int, aes(year, doy)) +
geom_point(colour = "steelblue4", alpha = 0.8) +
geom_ribbon(aes(ymin = lower_pi, ymax = upper_pi), alpha = 0.9) +
labs(y = "Day in year", x = "year",
title = paste(nr_knots, " knots, prior on weights ~ N(0,", vl_sigma, ")")) +
theme_minimal()}
cherry_spliner(nr_knots = 15, vl_sigma = 10)
cherry_spliner(nr_knots = 20, vl_sigma = 10)
cherry_spliner(nr_knots = 30, vl_sigma = 10)
# By increasing the prior or weights, we make our trend line more curved as well. This makes sense as the weights get be closer to 0 and therefore wiggle around closer to the mean line if the standard deviation is small. If the weights become larger, we then allow the curve to have more peaks