Warning: there is no package called 'RODBC'
Warning: there is no package called 'gridExtra'
Data for a single person
ds <- read.table(header = TRUE, text ="
obs id year attend
1 47 2000 4
2 47 2001 5
3 47 2002 6
4 47 2003 7
5 47 2004 6
6 47 2005 5
7 47 2006 4
8 47 2007 3
9 47 2008 2
10 47 2009 1
11 47 2010 2
12 47 2011 3
")
ds<- ds %>% dplyr::mutate(time=year-2000)
print(ds)
obs id year attend time
1 1 47 2000 4 0
2 2 47 2001 5 1
3 3 47 2002 6 2
4 4 47 2003 7 3
5 5 47 2004 6 4
6 6 47 2005 5 5
7 7 47 2006 4 6
8 8 47 2007 3 7
9 9 47 2008 2 8
10 10 47 2009 1 9
11 11 47 2010 2 10
12 12 47 2011 3 11
Let’s define a visual theme to keep graphs sharp, but consistent
plotTheme <- ggplot2::theme_bw() +
ggplot2::theme_bw(base_size=baseSize)+
ggplot2::theme(title=ggplot2::element_text(colour="gray20",size = 12)) +
ggplot2::theme(axis.text=ggplot2::element_text(colour="gray40"))+
ggplot2::theme(axis.title=ggplot2::element_text(colour="gray40"))+
ggplot2::theme(panel.border = ggplot2::element_rect(colour="gray80"))+
ggplot2::theme(axis.ticks.length = grid::unit(0, "cm"))
require(ggplot2)
p<-ggplot(ds, aes(x=time,y=attend, color=idF))
p<-p+ geom_line(color="purple",linetype=2, size=.5)
# p<-p+ geom_smooth(method=lm,stat="identity", color="purple")
p<-p+ geom_point(color="grey", size=2)
p<-p+ scale_y_continuous("Church attendance",
limits=c(0, 8),
breaks=c(1:8))
p<-p+ scale_x_continuous("Years since 2000",
limits=c(0,11),
breaks=c(0:11))
# annotations
p<-p+ labs(title=paste0("In the past year, how often have you attended a worship service?"))
p<- p + guides(color=guide_legend)
# themes
p <- p + plotTheme
p
add a straight line to represent possible predition line, in this case a straight line
linear<- predict(lm(attend ~ time, ds))
ds<- ds %>% dplyr::mutate(linear=linear)
print(ds)
obs id year attend time linear
1 1 47 2000 4 0 6.077
2 2 47 2001 5 1 5.699
3 3 47 2002 6 2 5.322
4 4 47 2003 7 3 4.944
5 5 47 2004 6 4 4.566
6 6 47 2005 5 5 4.189
7 7 47 2006 4 6 3.811
8 8 47 2007 3 7 3.434
9 9 47 2008 2 8 3.056
10 10 47 2009 1 9 2.678
11 11 47 2010 2 10 2.301
12 12 47 2011 3 11 1.923
p<-p+ geom_line(aes(y=linear),color="red", size=.5)
p
Or adding the curvarture the quadratic term
quadratic<- predict(lm(attend ~ poly(time,2),ds))
ds<- ds %>% mutate(quadratic=quadratic)
print(ds)
obs id year attend time linear quadratic
1 1 47 2000 4 0 6.077 5.335
2 2 47 2001 5 1 5.699 5.362
3 3 47 2002 6 2 5.322 5.308
4 4 47 2003 7 3 4.944 5.173
5 5 47 2004 6 4 4.566 4.958
6 6 47 2005 5 5 4.189 4.661
7 7 47 2006 4 6 3.811 4.283
8 8 47 2007 3 7 3.434 3.825
9 9 47 2008 2 8 3.056 3.285
10 10 47 2009 1 9 2.678 2.665
11 11 47 2010 2 10 2.301 1.964
12 12 47 2011 3 11 1.923 1.181
p<-p+ geom_line(aes(y=quadratic),color="blue", size=.5)
p
# p<-p+ geom_line(aes(y=cubic),color="green", size=.5)
or the cubic term
cubic<- predict(lm(attend ~ poly(time,3),ds))
ds<- ds %>% mutate( cubic=cubic)
print(ds)
obs id year attend time linear quadratic cubic
1 1 47 2000 4 0 6.077 5.335 3.604
2 2 47 2001 5 1 5.699 5.362 5.519
3 3 47 2002 6 2 5.322 5.308 6.410
4 4 47 2003 7 3 4.944 5.173 6.485
5 5 47 2004 6 4 4.566 4.958 5.954
6 6 47 2005 5 5 4.189 4.661 5.028
7 7 47 2006 4 6 3.811 4.283 3.916
8 8 47 2007 3 7 3.434 3.825 2.828
9 9 47 2008 2 8 3.056 3.285 1.974
10 10 47 2009 1 9 2.678 2.665 1.563
11 11 47 2010 2 10 2.301 1.964 1.806
12 12 47 2011 3 11 1.923 1.181 2.912
p<-p+ geom_line(aes(y=cubic),color="green", size=.5)
p