d <- data.frame(x = runif(100, -1, 1))
a <- 0
b <- sample(c(-1, 1), 1) * sample(c(0, 0.6, 0.9), 1)
d$y <- a + b * d$x + rnorm(100, sd = 0.25)
write.csv(d, "regression.csv", row.names = FALSE, quote = FALSE)

m <- lm(y ~ x, data = d)
bhat <- coef(m)[2]
bpvl <- summary(m)$coefficients[2, 4]
bsol <- c(bpvl >= 0.05, (bpvl < 0.05) & (bhat > 0), (bpvl < 0.05) & (bhat < 0))

Question

Using the data provided in regression.csv estimate a linear regression of y on x and answer the following questions.

Answerlist

Solution

\

plot(y ~ x, data = d)
abline(m)
legend(if(bhat > 0) "topleft" else "topright", bty = "n",
  paste0("b = ", fmt(bhat, 3), "\np = ", fmt(bpvl, 3)))

To replicate the analysis in R:

## data
d <- read.csv("regression.csv")
## regression
m <- lm(y ~ x, data = d)
summary(m)
## visualization
plot(y ~ x, data = d)
abline(m)

Meta-information

exname: Linear regression extype: cloze exsolution: r mchoice2string(bsol)|r fmt(bhat, 3) exclozetype: schoice|num extol: 0.01



Try the exams package in your browser

Any scripts or data that you put into this service are public.

exams documentation built on Nov. 14, 2022, 3:02 p.m.