First, let’s create some data and analyze it:
####class example
library(ggplot2)
height<-c(62,67.8,67.2,68.4,63.6,64.8,69,68)
age<-c(99,65,79,75,74,81,60,62)
dat.class<-data.frame(height,age)
dat.class
fit.height<-lm(height ~ age, data=dat.class)
summary(fit.height)
Call:
lm(formula = height ~ age, data = dat.class)
Residuals:
Min 1Q Median 3Q Max
-2.8114 -0.3977 -0.2023 0.6250 2.1523
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 78.5203 3.6373 21.587 6.45e-07 ***
age -0.1636 0.0483 -3.388 0.0147 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 1.616 on 6 degrees of freedom
Multiple R-squared: 0.6567, Adjusted R-squared: 0.5995
F-statistic: 11.48 on 1 and 6 DF, p-value: 0.01471
Check out residual values and then predicted values (i.e., y^)
dat.class$residuals<- fit.height$residuals
dat.class$residuals
[1] -0.32050452 -0.08407188 1.60680880 2.15227146 -2.81136288 -0.46592254 0.29775645 -0.37497489
dat.class$predicted <- predict(fit.height)
dat.class$predicted
[1] 62.32050 67.88407 65.59319 66.24773 66.41136 65.26592 68.70224 68.37497
scatterplot
ggplot(dat.class, aes(x = age, y = height)) + theme_bw()+theme(axis.text.x = element_text(size=18),axis.text.y = element_text(size=18),axis.title.x = element_text(face="bold",size=22),axis.title.y = element_text(face="bold",size=22))+
geom_point(pch=18,size=2.5)
scatterplot with y^’s and regression line
ggplot(dat.class, aes(x = age, y = height)) + theme_bw()+theme(axis.text.x = element_text(size=18),axis.text.y = element_text(size=18),axis.title.x = element_text(face="bold",size=22),axis.title.y = element_text(face="bold",size=22))+
geom_point(pch=18,size=2.5)+
geom_point(aes(y = predicted), shape = 1,size=2.5)+geom_smooth(method=lm,se=FALSE,color="grey",size=1.2)
scatterplot with residual error
ggplot(dat.class, aes(x = age, y = height)) + theme_bw()+theme(axis.text.x = element_text(size=18),axis.text.y = element_text(size=18),axis.title.x = element_text(face="bold",size=22),axis.title.y = element_text(face="bold",size=22))+
geom_point(pch=18,size=2.5) +geom_smooth(method=lm,se=FALSE,color="grey",size=1.2)+
geom_segment(aes(xend = age, yend = predicted),alpha=.6,size=1)