Solutions to Day 10 Homework

Exercise 1.11 in the STAT2 book:

a. Get data:

setwd("/Users/traves/Dropbox/sm339/day10/")
Volts <- read.csv("Volts.csv")
summary(Volts)
##     Voltage          Time      
##  Min.   :1.24   Min.   :0.000  
##  1st Qu.:1.96   1st Qu.:0.245  
##  Median :3.20   Median :0.490  
##  Mean   :3.87   Mean   :0.490  
##  3rd Qu.:5.38   3rd Qu.:0.735  
##  Max.   :9.21   Max.   :0.980
attach(Volts)
plot(Voltage ~ Time, main = "Voltage versus Time", ylab = "Voltage in Volts", 
    xlab = "Time in seconds")

plot of chunk unnamed-chunk-1

The scatterplot shows voltage decreasing over time but the decrease does not look linear – possibly logistic.

b. Transform data:

logv = log(Voltage)
plot(logv ~ Time, main = "Log Voltage versus Time", ylab = "Log Voltage in Volts", 
    xlab = "Time in seconds")

plot of chunk unnamed-chunk-2

Whoa. The transformed variable looks like it varies linearly with time.

c. Regression:

line = lm(logv ~ Time)
summary(line)
## 
## Call:
## lm(formula = logv ~ Time)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -0.02045 -0.01508 -0.00362  0.01219  0.04321 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  2.18994    0.00464     472   <2e-16 ***
## Time        -2.05907    0.00815    -253   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 
## 
## Residual standard error: 0.0166 on 48 degrees of freedom
## Multiple R-squared: 0.999,   Adjusted R-squared: 0.999 
## F-statistic: 6.38e+04 on 1 and 48 DF,  p-value: <2e-16

Equation of the regression line is

log(Voltage) = 2.189945 - 2.059065*Time.

d. Plot residuals vs. fitted values:

plot(line$residuals ~ line$fitted)

plot of chunk unnamed-chunk-4

Wow. There is clearly a quadratic pattern in the residuals (as plotted against the fitted values). Note that the data with lower and higher fitted values are higher than the model predicts and the data with fitted values near the mean are lower than the model predicts. All the same, the model seems to fit the data pretty well. Here's a plot of the transformed curve.

plot(Voltage ~ Time, main = "Voltage versus Time", ylab = "Voltage in Volts", 
    xlab = "Time in seconds")
o = order(Time)
lines(Time[o], exp(line$fitted[o]), col = "red")

plot of chunk unnamed-chunk-5