[R] Plotting linear fit
Rui Barradas
ruipbarradas at sapo.pt
Fri Jun 22 15:42:05 CEST 2012
Hello,
Try
test <- read.table(text="
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
", header=TRUE)
fit <- by(test, test$id, function(dat) fitted.values(lm(incidents ~
year, data=dat)))
fit <- unclass(fit)
with(test, interaction.plot(year, id, unlist(fit), col=seq_along(fit)))
Also, it's better to post datasets using dput(): just copy its output
and paste it.
Hope this helps,
Rui Barradas
Em 22-06-2012 13:20, Van Patten, Isaac T escreveu:
> 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
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list