This chapter introduced the simple linear regression model, a framework for estimating the association between a predictor variable and an outcome variable. The Gaussian distribution comprises the likelihood in such models, because it counts up the relative numbers of ways different combinations of means and standard deviations can produce an observation. To fit these models to data, the chapter introduced quadratic approximation of the posterior distribution and the tool quap. It also introduced new procedures for visualizing prior and posterior distributions.
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. Problems are labeled Easy (E), Medium (M), and Hard(H).
Finally, upon completion, name your final output .html file as: YourName_ANLY505-Year-Semester.html and publish the assignment to your R Pubs account and submit the link to Canvas. Each question is worth 5 points.
4E1. In the model definition below, which line is the likelihood? \[\begin{align} \ y_i ∼ Normal(μ, σ) \\ \ μ ∼ Normal(0, 10) \\ \ σ ∼ Exponential(1) \\ \end{align}\]
# \ y_i ∼ Normal(μ, σ) \\
4E2. In the model definition just above, how many parameters are in the posterior distribution?
# 2 (μ, σ)
4E3. Using the model definition above, write down the appropriate form of Bayes’ theorem that includes the proper likelihood and priors.
# Pr(μ,σ|y) =(ΠiNormal(yi|μ, σ)Normal(μ|0,10)Uniform(σ|0,10))/ ∫∫ΠiNormal(hi|μ, σ)Normal(μ|0,10)Uniform(σ|0,10)dμdσ
4E4. In the model definition below, which line is the linear model? \[\begin{align} \ y_i ∼ Normal(μ, σ) \\ \ μ_i = α + βx_i \\ \ α ∼ Normal(0, 10) \\ \ β ∼ Normal(0, 1) \\ \ σ ∼ Exponential(2) \\ \end{align}\]
## \ μi=α + βx i\\
4E5. In the model definition just above, how many parameters are in the posterior distribution?
# α, β, and σ.
4M1. For the model definition below, simulate observed y values from the prior (not the posterior). Make sure to plot the simulation. \[\begin{align} \ y_i ∼ Normal(μ, σ) \\ \ μ ∼ Normal(0, 10) \\ \ σ ∼ Exponential(1) \\ \end{align}\]
mu_sample <- rnorm(1e4, 0, 100)
sigma_sigma <- runif(1e4, 0, 100)
prior_y <- rnorm(1e4, mu_sample, sigma_sigma)
#densplot(prior_y)
4M2. Translate the model just above into a quap formula.
formula <- alist(
y ~ dnorm(mu, sigma),
mu ~ dnorm(0, 100),
sigma ~ dunif(0, 100)
)
4M3. Translate the quap model formula below into a mathematical model definition:
y ~ dnorm( mu , sigma ),
mu <- a + b*x,
a ~ dnorm( 0 , 10 ),
b ~ dunif( 0 , 1 ),
sigma ~ dexp( 1 )
flist <- alist(
y ~ dnorm(mu, sigma),
mu <- a + b*x,
a ~ dnorm(0, 50),
b ~ dunif(0, 10),
sigma ~ dunif(0, 50)
)
4M4. A sample of students is measured for height each year for 3 years. After the third year, you want to fit a linear regression predicting height using year as a predictor. Write down the mathematical model definition for this regression, using any variable names and priors you choose. Be prepared to defend your choice of priors. Simulate from the priors that you chose to see what the model expects before it sees the data. Do this by sampling from the priors. Then consider 50 students, each simulated with a different prior draw. For each student simulate 3 years. Plot the 50 linear relationships with height(cm) on the y-axis and year on the x-axis. What can we do to make these priors more likely?
# hi ∼ Normal(μi,σ)
# μi = α + βt i
# α ∼ Normal(120,20)
# β ∼ Normal(0,10)
# σ ∼ Uniform(0,50)
4M5. Now suppose I remind you that every student got taller each year. Does this information lead you to change your choice of priors? How? Again, simulate from the priors and plot.
# No.
4M6. Now suppose I tell you that the variance among heights for students of the same age is never more than 64cm. How does this lead you to revise your priors?
# Variance term can be adjust by introducing an uniform distribution
4M7. Refit model m4.3 from the chapter, but omit the mean weight xbar this time. Compare the new model’s posterior to that of the original model. In particular, look at the covariance among the parameters. Show the pairs() plot. What is different? Then compare the posterior predictions of both models.
#data(Howell1)
#d1 <- Howell1
#d2 <- d1[d1$age>=18,]
#m4.3 <- quap(
# alist(
# height~dnorm(mu,sigma),
# mu<-a+b*(weight),
#a~dnorm(178,20),
#b~dlnorm(0,1),
#sigma~dunif(0,50)
# ),
# data=d2)
#precis(m4.3)
4M8. In the chapter, we used 15 knots with the cherry blossom spline. Increase the number of knots and observe what happens to the resulting spline. Then adjust also the width of the prior on the weights—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)
#df <- cherry_blossoms
#precis(df)
#q4m8 <- df[complete.cases(cherry_blossoms$doy),]
#numknots15 <- 15
#knot15 <- quantile(q4m8$year, probs = seq(0, 1, length.out=numknots15))
#knot15
#numknots30 <- 30
#knot30 <- quantile(q4m8$year, probs = seq(0, 1, length.out = numknots30))
#knot30
4H2. 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?
Plot the raw data, with height on the vertical axis and weight on the horizontal axis. Superimpose the MAP regression line and 89% interval for the mean. Also superimpose the 89% interval for predicted heights.
What aspects of the model fit concern you? Describe the kinds of assumptions you would change, if any, to improve the model. You don’t have to write any new code. Just explain what the model appears to be doing a bad job of, and what you hypothesize would be a better model.
#a
#qhdata <- Howell1[Howell1$age<18,]
#nrow(qhdata)
#b
#plot(height ~ weight, data = qhdata)
# (c)
# I am concerned with the skewness of the data. Might need to log transform or normalize it and check.