R For SAS Users
Melinda Higgins, PhD
Research Professor/Senior Biostatistician Emory University
Run regression models
Choose best models
Evaluate and compare models
Report best associations
# Run lm() for diffht by bmi, save model
lmdiffhtbmi <- lm(diffht ~ bmi,
data = daviskeep)
# Run lm() for diffht by weight, save model
lmdiffhtwt <- lm(diffht ~ weight,
data = daviskeep)
# Run summary() for each model, save results
smrylmdiffhtbmi <- summary(lmdiffhtbmi)
smrylmdiffhtwt <- summary(lmdiffhtwt)
# Display r.squared for weight model
smrylmdiffhtwt$r.squared
# Display r.squared for bmi model
smrylmdiffhtbmi$r.squared
# Compare AICs for both models
AIC(lmdiffhtbmi, lmdiffhtwt)
[1] 0.003281645
[1] 0.00121824
df AIC
lmdiffhtbmi 3 788.0816
lmdiffhtwt 3 787.7052
# Plot diffht by weight by sex
ggplot(daviskeep,
aes(diffht, weight)) +
geom_point() +
geom_smooth(method = "lm") +
facet_wrap(vars(sex)) +
ggtitle("Height differences
predicted by weight,
model fit by sex")
# lm() of diffht by weight for females
lmdiffhtwtF <- lm(diffht ~ weight,
subset = (sex == "F"),
data = daviskeep)
# lm() of diffht by weight for males
lmdiffhtwtM <- lm(diffht ~ weight,
subset = (sex == "M"),
data = daviskeep)
# Run summary() for each model save results
smrylmdiffhtwtF <- summary(lmdiffhtwtF)
smrylmdiffhtwtM <- summary(lmdiffhtwtM)
# r.squared for females only model
smrylmdiffhtwtF$r.squared
# r.squared for males only model
smrylmdiffhtwtM$r.squared
[1] 4.00807e-05
[1] 0.00804139
R For SAS Users