Worksheets/Week9
Revision as of 20:34, 4 May 2019 by Kevin Dunn (talk | contribs) (Created page with "=== Part 1 === Description here <html><div data-datacamp-exercise data-lang="r" data-height="auto"> <code data-type="sample-code"> center = 36 range = 24 rw.x <- c(24,...")
Part 1
Description here
center = 36
range = 24
rw.x <- c(24, 48)
coded.x <- (rw.x - center)/(0.5*range)
y0 <- c(28, 63)
model.0 <- lm(y0 ~ coded.x)
summary(model.0)
# What is the interpretation of the
# * slope?
# * intercept
# * why are R2 and SE where they are?
# Basic plot of everything so far:
raw_data <- data.frame(coded.x = coded.x, y = y0)
library(ggplot2)
p <- ggplot(data=raw_data, aes(x=coded.x, y=y)) +
geom_point(size=5) +
xlab("Coded value for x_A") +
scale_x_continuous(breaks=seq(-2, 5, 1)) +
ylab("Outcome variable") +
scale_y_continuous(breaks=seq(0, 150, 10)) +
theme_bw() +
theme(axis.text=element_text(size=18), legend.position = "none") +
theme(axis.title=element_text(face="bold", size=14))
p
# Run experiment at center point: predict it first
rw.x <- c(24, 48, 36)
coded.x <- (rw.x - center)/(0.5*range)
x.test <- data.frame(coded.x=coded.x)
predict(model.0, newdata=x.test)
# Expect a predicted value of 45.5 in the output. Actual: 54 and 55. So about
# 10 units difference.
# Add the linear fit: through the 2 points
plot_data <- data.frame(coded.x = seq(-2, +5, 0.1))
plot_data$y <- predict(model.0, newdata=plot_data)
p <- p + geom_line(data=plot_data, color="blue", size=1)
p
# Try fitting a linear model now through all the data points:
rw.x <- c(24, 48, 36, 36)
y1 <- c(28, 63, 55, 54)
coded.x <- (rw.x - center)/(0.5*range)
model.1 <- lm(y1 ~ coded.x)
summary(model.1)
# Show the linear fit with the extra data point (center points)
raw_data <- data.frame(coded.x = coded.x, y = y1)
plot_data$y <- predict(model.1, newdata=plot_data)
p <- p + geom_point(aes(x=coded.x, y=y, color='darkgreen', size=5), data=raw_data)
p <- p + geom_line(data=plot_data, color="darkgreen", size=1)
p
# A a quadratic component through all the data points:
rw.x <- c(24, 48, 36, 36)
y1 <- c(28, 63, 55, 54)
coded.x <- (rw.x - center)/(0.5*range)
model.1.quad <- lm(y1 ~ coded.x + I(coded.x^2))
summary(model.1.quad)
# Show the quadratic fit between -2 and +2 in coded units
# In real-world units this corresonds to _____ and _____
plot_data <- data.frame(coded.x = seq(-2, +3, 0.1))
plot_data$y <- predict(model.1.quad, newdata=plot_data)
p <- p + geom_line(data=plot_data, color="red", size=1)
p
# Try a new point at +2: that is RW = coded * 0.5 * range + center; 60 hours
# y = 66.
y0 <- c(28, 63)
rw.x <- c(24, 48, 36, 36, 60)
y2 <- c(28, 63, 55, 54, 66)
coded.x <- (rw.x - center)/(0.5*range)
model.2.quad <- lm(y2 ~ coded.x + I(coded.x^2))
summary(model.2.quad)
# Plot it again:
DO: add the new point and response here
plot_data <- data.frame(coded.x = seq(-2, +3, 0.1))
plot_data$y <- predict(model.2.quad, newdata=plot_data)
p <- p + geom_line(data=plot_data, color="purple", size=1)
p