# Interpretation.R
#
# This script implements the examples in the 'interpretation
# of regression' tutorial.
# First, we'll load in the data from the tutorial:
mydata <-read.csv("http://www.courses.washington.edu/psy315/datasets/TemperatureAttendance.csv")
# We'll follow the procedure in the 'prediction.R' script to find
# the correlation, slope, intercept and syx for this data set.
# Define 'x' to be the temperature and y be the attendance:
x <- mydata$Temperature
y <- mydata$Attendance
# The scatterplot can now be done by plotting x vs y:
plot(x,y,
xlab = "Temperature",
ylab = "Attendance",
pch = 19,
col = "blue",
cex = 1)
# draw the regression line
abline(lm(y~x))
# Calculate the correlation:
r <- cor(x,y,use = "complete.obs")
print(r)
# The slope of the regression line is:
m <- r*sd(y)/sd(x)
# The intercept is:
b <- mean(y) - m*mean(x)
# Calculate the standard error of the estimate, syx:
syx <- sd(y)*sqrt(1-r^2)
print(syx)
# We're now ready to do the examples in the tutorial.
###
# Example 1: What is the expected attendance when the
# outdoor temperature is 70?
# For this we just find the value on the reguression line
# where x = 70:
print(70*m+b)
###
# Example 2: What is the temperature for which the
# expected attendance is 60:
# For this we find x for y=60 on the regression line
# if y = m*x+b, then x = (y-b)/m
print((60-b)/m)
###
# Example 3: On a day that the outdoor temperature is
# 66.7, what is the probability that 80 or more students
# show up for class
# We know from Example 2 that if the temperature is 66.7
# then attendance will be distributed normally with a mean
# of 60 and the standard deviaton will be syx.
# The probability of 80 or more showing up can be found
# using 'pnorm' (see NormalDistribution.R):
print(1-pnorm(80,60,syx))
###
# Example 4
#
# What does the outdoor temperature have to be so that there
# is a 75% chance of 60 or more students showing up?
# The z-score for which 75% of the area lies above is:
z <- -qnorm(.75)
# The attendance that is z standard deviations above 60 is:
yprime <- 60-syx*z
# The temperature for this value of y is (see Example 2:)
print((yprime-b)/m)