[R] Plotting linear fit

Van Patten, Isaac T ivanpatt at RADFORD.EDU
Fri Jun 22 14:20:03 CEST 2012


I am trying to plot the linear fit by id of the following data (test.l):

 id year incidents
100    1         0
101    1         1
102    1        21
103    1        27
104    1         3
105    1        12
100    2         5
101    2         5
102    2        19
103    2        10
104    2         2
105    2        12
100    3         0
101    3         0
102    3        22
103    3        14
104    3        16
105    3        13

I am using this code:

fit <- by(test.l, test.l$id, function(data) fitted.values(lm(incidents ~ year, data=data)))  
fit1 <- unlist(fit)
names(fit1) <- NULL
interaction.plot(test.l$year, test.l$id, fit1, xlab="Year", ylab="Incidents", ylim=c(0, 40))

Instead of a continuous linear fit, I get a linear fit from year to year.  I am looking to plot
fit over the entire span of time.  What am I doing wrong?

-Isaac



More information about the R-help mailing list