Sunday, 15 July 2012

plot - Regression coefficients and abline in R - linear regression -



plot - Regression coefficients and abline in R - linear regression -

thanks in advance attention. here it's problem:

i have dataframe, it's construction (i have deleted rows):

date cases 02/01/2013 1 02/01/2013 2 03/01/2013 3 04/01/2013 4 04/01/2013 5 08/01/2013 6 08/01/2013 7 16/01/2013 8 18/01/2013 9 18/01/2013 10 18/01/2013 11 21/01/2013 12 22/01/2013 13 23/01/2013 14 23/01/2013 15 23/01/2013 16 23/01/2013 17 23/01/2013 18 23/01/2013 19 24/01/2013 20 24/01/2013 21 24/01/2013 22 30/01/2013 23 30/01/2013 24

and do:

d<-read.csv("...",sep=";") model<-lm(d$cases~d$date) plot(sort(d[,1]),d[,2]) abline(model)

after lastly command i've got message:

abline: using first 2 of 11 regression coefficients

it works "fine" little dataset not create sense if work whole dataset.

this plot (using finish dataset): http://imgur.com/vhnpdil

if want try, finish one:

https://www.dropbox.com/s/qrd9lt4r7gs1r98/regresion.csv

i know if i'm doing wrong because have no idea. abline work fine?

thank again!

your date variable factor (or perchance character). need recode numeric - careful right , not internal factor coding, recode first date, numeric.

read data:

d <- read.csv("regresion.csv",sep=";")

convert dates (which interpreted unstructured strings , automatically converted factors read.csv()):

d$fecha <- as.date(d$fecha,format="%d/%m/%y")

for regression, convert these numeric:

date.num <- as.numeric(d$fecha) date.num

run regression:

model <- lm(d$alertas.europa~date.num)

plot - note plot create sense, 1 time again need x axis (fecha) date, why converted above:

plot(d$fecha,d$alertas.europa)

finally, add together regression line:

abline(model,col="red",lwd=2)

r plot dataset linear-regression

No comments:

Post a Comment