-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathrms2-5.Rmd
69 lines (65 loc) · 1.86 KB
/
rms2-5.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
---
title: "RMS Chapter 2 Problem 5"
author: "your name"
date: "`r Sys.Date()`"
output:
html_document:
highlight: pygments
toc: yes
major: regression modeling; rms
minor: curve fitting
---
```{r setup}
require(Hmisc)
knitrSet(lang='markdown')
```
# SAT Data
```{r define-data}
pct <- c( 4, 5, 5, 6, 6, 7, 9, 9, 10, 10, 10, 10, 12, 12, 13, 13,
14, 14, 14, 16, 17, 18, 20, 22, 23,
24, 29, 37, 43, 44, 45, 49, 50, 52, 55, 57, 58, 59, 60,
62, 63, 63, 63, 64, 64, 68, 69,
72, 73, 81)
score <-
c(482,498,513,498,511,479,480,483,475,476,487,494,474,478,457,
485,451,471,473,467,470,464,471,455,452,440,460,448,441,424,
417,422,441,408,412,400,401,430,433,433,404,424,430,
431,437,446,424,420,432,436)
# Prepare rms so that it will use good limits for plotting
require(rms)
dd <- datadist(pct); options(datadist='dd')
```
# Linear Spline Fit
```{r linear-spline}
f <- ols(score ~ lsp(pct, 50))
f
ap <- geom_point(aes(x=pct, y=score), data.frame(pct, score))
# Take charge of x-axis limits for plotting predicted values so will
# show entire range of data
pcts <- seq(0,80,length=150)
ggplot(Predict(f, pct=pcts)) + ap
```
# Restricted Cubic Spline Fit
```{r rcs}
f <- ols(score ~ rcs(pct, c(6, 12, 58, 68)))
f
ggplot(Predict(f, pct=pcts)) + ap
f.linear <- ols(score ~ pct)
# Retrieve R^2 from the cubic spline fit and from the reduced model (linear)
R2 <- f$stats['R2']
R2red <- f.linear$stats['R2']
# Compute F statistic using difference in R^2 and R^2 of full model
# df.num <- ...
# df.denom <- ...
# Fstat <- ...
# pvalue <- 1 - pf(Fstat, df.num, df.denom)
# c(df.num=df.num, df.denom=df.denom, Fstat=Fstat, P=pvalue) # prints all stats
```
# Computing Environment
```{r rsession,echo=FALSE}
si <- sessionInfo(); si$loadedOnly <- NULL
print(si, locale=FALSE)
```
```{r cite,results='asis',echo=FALSE}
print(citation(), style='text')
```