2

Thanks in advance for your attention. Here it's my problem:

I have a dataframe, this is it's structure (I have deleted some 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 this is what I do:

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

After the last command I've got this message:

abline: only using the first two of 11 regression coefficients

It works "fine" with a small dataset but it does not make any sense if I work with the whole dataset.

This is the plot I get (using the complete dataset): https://i.stack.imgur.com/7YL3O.jpg

If you want to try, this is the complete one:

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

I would like to know if I'm doing something wrong because I have no idea. Does abline work fine?

Thank you again!

Eka
  • 47
  • 1
  • 7

1 Answers1

3

Your DATE variable is a factor (or possibly a character). You need to recode it to numeric - but be careful to do this right and not get the internal factor coding, so recode first as Date, then as numeric.

Read the data:

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

Convert your dates (which were interpreted as unstructured strings and automatically converted to factors by read.csv()):

d$FECHA <- as.Date(d$FECHA,format="%d/%m/%Y")

For regression, convert these to numeric:

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

Run the regression:

model <- lm(d$ALERTAS.EUROPA~date.num)

Plot - note that for the plot to make sense, we again need the x axis (FECHA) to be a Date, which is why we converted it above:

plot(d$FECHA,d$ALERTAS.EUROPA)

Finally, add the regression line:

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

plot

Stephan Kolassa
  • 7,953
  • 2
  • 28
  • 48