####Review question 1.1
R version 3.0.3 (2014-03-06) -- "Warm Puppy"
Copyright (C) 2014 The R Foundation for Statistical Computing
Platform: x86_64-w64-mingw32/x64 (64-bit)
# I haven't moved to most recent R , yet
# get the sleepstudy data and pick off our trucker #372
> library(lme4)
Loading required package: lattice
Loading required package: Matrix
> data(sleepstudy)
> sdat = subset(sleepstudy, Subject == "372")
> sdat
Reaction Days Subject
171 269.4117 0 372
172 273.4740 1 372
173 297.5968 2 372
174 310.6316 3 372
175 287.1726 4 372
176 329.6076 5 372
177 334.4818 6 372
178 343.2199 7 372
179 369.1417 8 372
180 364.1236 9 372
> attach(sdat)
#create a vector (9-tuple) of first-difference (day to day change)
> ?diff
starting httpd help server ... done
> diffs = diff(Reaction)
> diffs
[1] 4.0623 24.1228 13.0348 -23.4590 42.4350 4.8742 8.7381 25.9218 -5.0181
#under the constant rate of change model, each of these differences estimates the rate of
# change (impairment) of reaction time
#easy way to get a mean and CI for the first differences.
> t.test(diffs)
One Sample t-test
data: diffs
t = 1.6525, df = 8, p-value = 0.137
alternative hypothesis: true mean is not equal to 0
95 percent confidence interval:
-4.161587 25.208675
sample estimates:
mean of x
10.52354
#repeat the OLS fit from class handout
> lm.sdat = lm(Reaction ~ Days)
> coef(lm.sdat)
(Intercept) Days
267.04480 11.29807
> confint(lm.sdat)
2.5 % 97.5 %
(Intercept) 251.750906 282.33869
Days 8.433264 14.16288
# so mean of the diffs is not far off from OLS slope. But diffs far
less efficient. OLS gives a much tighter CI
#here's a useful plot, first differences compared with OLS slope
> k= 1:9; plot(k,diffs)
> ?abline
> abline(10.52,0)
>