Title: ‘Assignment #3’ Author: “Cem Soylu” Date: “2022-08-17” Output: html_document

Week 3 - Basic Regression

This chapter (Chapter 4) 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.

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.

Questions

4-1. 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}\]

set.seed(2022)

# simulate prior mean and standard deviation for y
prior_mu <- rnorm(10000, 0, 10)
prior_sigma <- rexp(10000, 1)

# simulate observed y values from the prior
prior_y <- rnorm(10000, prior_mu, prior_sigma) %>% enframe()

# plot the simulation
ggplot(prior_y) +
  geom_density(aes(value), size = 1) + 
  theme_light()

4-2. Translate the model just above into a quap formula.

quap_formula <- alist(y ~ dnorm(mu, sigma), 
                 mu ~ dnorm(0, 10), 
                 sigma ~ dexp(1)) 
4-3. 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))

flist
## [[1]]
## y ~ dnorm(mu, sigma)
## 
## [[2]]
## mu <- a + b * x
## 
## [[3]]
## a ~ dnorm(0, 50)
## 
## [[4]]
## b ~ dunif(0, 10)
## 
## [[5]]
## sigma ~ dunif(0, 50)

4-4. 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 chose a linear model without any polynomial terms or transformations. 

# μ = α + βxi where xi is the year
# α ∼ Normal(150,25) 
# β ∼ Normal(4,2) 
# σ ∼ Uniform(0,50)

set.seed(2022)

# I chose the conservative priors to begin with

alpha_prior <- rnorm(50,150,25)
beta_prior <- rnorm(50,4,2)
sigma_prior <- runif(50,0,50)
year <- seq(from =1, to=3, len=3)

plot(type = 'l', x=c(1,3),y=c(80,200),
     xlab ="Years", ylab = "Height(cm)", main = "Height vs. Year", col = 'dark red')
for (i in 1:50)
  lines(year, alpha_prior[i] + beta_prior[i]*year, lwd=3 , col=4)
  lines(year, alpha_prior[i] + sigma_prior[i]*year, lwd=3 , col=5)

4-5. (a) 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. (b) 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?

# hi ∼ Normal(μ, σ) 
# μ = α + βxi
# α ∼ Normal(120, 10) 
# β ∼ Normal(7, 1) 
# σ ∼ Uniform(0, 20)

set.seed(2022)
alpha_prior2 <- rnorm(50,120,10)
beta_prior2 <- rnorm(50,7,1)
sigma_prior2 <- runif(50,0,20)
year <- seq(from =1, to=3, len=3)

plot(type = 'l', x=c(1,3),y=c(80,200),
     xlab ="Years", ylab = "Height(cm)", main = "Height vs. Years", col = 'dark red')
for (i in 1:50)
  lines (year, alpha_prior2[i] + beta_prior2[i]*year , lwd=3 , col=4)
  lines(year, alpha_prior2[i] + sigma_prior2[i]*year, lwd=3 , col=5)

I centered the α prior around 120 cm and decreased its SD to 10 cm to reflect new knowledge about the average height. Then β prior was centered around 7 cm/year and decreased its SD to 1 cm/year as these values are more consistent with school age students. Lastly, maximum value in the σ prior was reduced to 20 cm because a low average height is more associated with a lower SD.

The variance is the square of σ, so if variance is never more than 64 cm, then σ is never more than 8 cm. Thus we may not need to revise the priors.