[R] Problems plotting and regression w.r.t. date data type on x axis

2013-02-14 Thread Kategoricus
Hello,

probably a newbie question, but i didnt find any information on
plotting/regressing w.r.t. a date data type. My trials were unfruitful. Can
anyone help ? Thanks in advance!

Here is my interaction with R:


 tabelle
date number date2
1 2009-01-1 1673 2009-01-01
2 2009-12-1 2111 2009-12-01
3 2010-7-1 2487 2010-07-01
4 2013-2-1 4301 2013-02-01
 regression.punkte-lm(tabelle$number ~ tabelle$date2)
Fehler in model.frame.default(formula = tabelle$number ~ tabelle$date2, :
ungültiger Typ (list) für die Variable 'tabelle$date2'
 regression.punkte-lm(tabelle$number ~ tabelle$date)
 plot(tabelle$date2,tabelle$number, pch=19, xlab=date of retrieval,
 ylab=number of animals) ***R DRAWS THIS***
 regression.punkte

Call:
lm(formula = tabelle$number ~ tabelle$date)

Coefficients:
(Intercept) tabelle$date2009-12-1 tabelle$date2010-7-1
1673 438 814
tabelle$date2013-2-1
2628

 abline(regression.punkte, lwd=2) ***R DOES NOT DRAW LINE***
Warnmeldung:
In abline(regression.punkte, lwd = 2) :
nutze nur die ersten beiden von 4 Regressionskoeffizienten
 summary(regression.punkte)

Call:
lm(formula = tabelle$number ~ tabelle$date)

Residuals:
ALL 4 residuals are 0: no residual degrees of freedom!

Coefficients:
Estimate Std. Error t value Pr(|t|)
(Intercept) 1673 NA NA NA
tabelle$date2009-12-1 438 NA NA NA
tabelle$date2010-7-1 814 NA NA NA
tabelle$date2013-2-1 2628 NA NA NA

Residual standard error: NaN on 0 degrees of freedom
Multiple R-squared: 1, Adjusted R-squared: NaN
F-statistic: NaN on 3 and 0 DF, p-value: NA



--
View this message in context: 
http://r.789695.n4.nabble.com/Problems-plotting-and-regression-w-r-t-date-data-type-on-x-axis-tp4658518.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Problems plotting and regression w.r.t. date data type on x axis

2013-02-14 Thread PIKAL Petr
Hi

most probably your dates are not what you expect. Eg. they look like a date but 
they are not treated as date. You can check yourself by

str(tabelle)

which will result probably in factor, numeric, factor.

You need to change variables date and date2 into Date class.

?strptime or ?as.Date

date and date2 seems to be same but usually you shall use the same variables 
for plotting and for model to get correct regression line. 

Regards
Petr

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of Kategoricus
 Sent: Thursday, February 14, 2013 10:10 AM
 To: r-help@r-project.org
 Subject: [R] Problems plotting and regression w.r.t. date data type on
 x axis
 
 Hello,
 
 probably a newbie question, but i didnt find any information on
 plotting/regressing w.r.t. a date data type. My trials were unfruitful.
 Can anyone help ? Thanks in advance!
 
 Here is my interaction with R:
 
 
  tabelle
 date number date2
 1 2009-01-1 1673 2009-01-01
 2 2009-12-1 2111 2009-12-01
 3 2010-7-1 2487 2010-07-01
 4 2013-2-1 4301 2013-02-01
  regression.punkte-lm(tabelle$number ~ tabelle$date2)
 Fehler in model.frame.default(formula = tabelle$number ~ tabelle$date2,
 :
 ungültiger Typ (list) für die Variable 'tabelle$date2'
  regression.punkte-lm(tabelle$number ~ tabelle$date)
  plot(tabelle$date2,tabelle$number, pch=19, xlab=date of retrieval,
  ylab=number of animals) ***R DRAWS THIS*** regression.punkte
 
 Call:
 lm(formula = tabelle$number ~ tabelle$date)
 
 Coefficients:
 (Intercept) tabelle$date2009-12-1 tabelle$date2010-7-1
 1673 438 814
 tabelle$date2013-2-1
 2628
 
  abline(regression.punkte, lwd=2) ***R DOES NOT DRAW LINE***
 Warnmeldung:
 In abline(regression.punkte, lwd = 2) :
 nutze nur die ersten beiden von 4 Regressionskoeffizienten
  summary(regression.punkte)
 
 Call:
 lm(formula = tabelle$number ~ tabelle$date)
 
 Residuals:
 ALL 4 residuals are 0: no residual degrees of freedom!
 
 Coefficients:
 Estimate Std. Error t value Pr(|t|)
 (Intercept) 1673 NA NA NA
 tabelle$date2009-12-1 438 NA NA NA
 tabelle$date2010-7-1 814 NA NA NA
 tabelle$date2013-2-1 2628 NA NA NA
 
 Residual standard error: NaN on 0 degrees of freedom Multiple R-
 squared: 1, Adjusted R-squared: NaN
 F-statistic: NaN on 3 and 0 DF, p-value: NA
 
 
 
 --
 View this message in context: http://r.789695.n4.nabble.com/Problems-
 plotting-and-regression-w-r-t-date-data-type-on-x-axis-tp4658518.html
 Sent from the R help mailing list archive at Nabble.com.
 
 __
 R-help@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.
__
R-help@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.


Re: [R] Problems plotting and regression w.r.t. date data type on x axis

2013-02-14 Thread David Winsemius


On Feb 14, 2013, at 1:10 AM, Kategoricus wrote:


Hello,

probably a newbie question, but i didnt find any information on
plotting/regressing w.r.t. a date data type. My trials were  
unfruitful. Can

anyone help ? Thanks in advance!

Here is my interaction with R:



tabelle

date number date2
1 2009-01-1 1673 2009-01-01
2 2009-12-1 2111 2009-12-01
3 2010-7-1 2487 2010-07-01
4 2013-2-1 4301 2013-02-01

regression.punkte-lm(tabelle$number ~ tabelle$date2)
Fehler in model.frame.default(formula = tabelle$number ~ tabelle 
$date2, :

ungültiger Typ (list) für die Variable 'tabelle$date2'


I'm guessing that you used as.POSIXlt to create that date2 variable.  
POSIXlt classed variables are lists and as such create all sorts of  
problems for functions that are expecting atomic vectors. If I'm  
right, you will ahve better chances of success by converting to  
POSIXct class.


 tabelle - read.table(text=date number date2
+ 1 2009-01-1 1673 2009-01-01
+ 2 2009-12-1 2111 2009-12-01
+ 3 2010-7-1 2487 2010-07-01
+ 4 2013-2-1 4301 2013-02-01, header=TRUE, stringsAsFactors=FALSE)
 tabelle$date2 - as.POSIXlt(tabelle$date2)
 regression.punkte-lm(tabelle$number ~ tabelle$date2)
Error in model.frame.default(formula = tabelle$number ~ tabelle 
$date2,  :

  invalid type (list) for variable 'tabelle$date2'

 tabelle$date2 - as.POSIXct(tabelle$date2)
 regression.punkte-lm(tabelle$number ~ tabelle$date2)
 regression.punkte

Call:
lm(formula = tabelle$number ~ tabelle$date2)

Coefficients:
  (Intercept)  tabelle$date2
   -2.405e+04  2.082e-05



regression.punkte-lm(tabelle$number ~ tabelle$date)
plot(tabelle$date2,tabelle$number, pch=19, xlab=date of retrieval,
ylab=number of animals) ***R DRAWS THIS***
regression.punkte


Call:
lm(formula = tabelle$number ~ tabelle$date)

Coefficients:
(Intercept) tabelle$date2009-12-1 tabelle$date2010-7-1
1673 438 814
tabelle$date2013-2-1
2628


abline(regression.punkte, lwd=2) ***R DOES NOT DRAW LINE***

Warnmeldung:
In abline(regression.punkte, lwd = 2) :
nutze nur die ersten beiden von 4 Regressionskoeffizienten

summary(regression.punkte)


Call:
lm(formula = tabelle$number ~ tabelle$date)

Residuals:
ALL 4 residuals are 0: no residual degrees of freedom!

Coefficients:
Estimate Std. Error t value Pr(|t|)
(Intercept) 1673 NA NA NA
tabelle$date2009-12-1 438 NA NA NA
tabelle$date2010-7-1 814 NA NA NA
tabelle$date2013-2-1 2628 NA NA NA

Residual standard error: NaN on 0 degrees of freedom
Multiple R-squared: 1, Adjusted R-squared: NaN
F-statistic: NaN on 3 and 0 DF, p-value: NA




David Winsemius, MD
Alameda, CA, USA

__
R-help@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.


Re: [R] Problems plotting and regression w.r.t. date data type on x axis

2013-02-14 Thread Kategoricus
Hello Petr!

thanks a lot for your help. The plot command plots well, but the abline
returns without error and does nothing.

 str(tabelle)
'data.frame':   4 obs. of  3 variables:
 $ date  : Factor w/ 4 levels 2009-01-1,2009-12-1,..: 1 2 3 4
 $ number: int  1673 2111 2487 4301
 $ date2 : POSIXlt, format: 2009-01-01 2009-12-01 ...
 plot(tabelle$date2,tabelle$number, pch=19, xlab=date of retrieval,
 ylab=number of animals)
 regression.punkte-lm(tabelle$number ~ as.Date(tabelle$date))
 regression.punkte

Call:
lm(formula = tabelle$number ~ as.Date(tabelle$date))

Coefficients:
  (Intercept)  as.Date(tabelle$date)  
   -24046.326  1.799  

 abline(regression.punkte, lwd=2)
 



--
View this message in context: 
http://r.789695.n4.nabble.com/Problems-plotting-and-regression-w-r-t-date-data-type-on-x-axis-tp4658518p4658529.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Problems plotting and regression w.r.t. date data type on x axis

2013-02-14 Thread Kategoricus
as.POSIXct() works fine (finally!)

Thanks a lot DavidPetr for your help!



--
View this message in context: 
http://r.789695.n4.nabble.com/Problems-plotting-and-regression-w-r-t-date-data-type-on-x-axis-tp4658518p4658537.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] Problems plotting and regression w.r.t. date data type on x axis

2013-02-14 Thread PIKAL Petr
Hi

 -Original Message-
 From: r-help-boun...@r-project.org [mailto:r-help-bounces@r-
 project.org] On Behalf Of Kategoricus
 Sent: Thursday, February 14, 2013 12:25 PM
 To: r-help@r-project.org
 Subject: Re: [R] Problems plotting and regression w.r.t. date data type
 on x axis
 
 Hello Petr!
 
 thanks a lot for your help. The plot command plots well, but the abline
 returns without error and does nothing.
 
  str(tabelle)
 'data.frame':   4 obs. of  3 variables:
  $ date  : Factor w/ 4 levels 2009-01-1,2009-12-1,..: 1 2 3 4  $
 number: int  1673 2111 2487 4301  $ date2 : POSIXlt, format: 2009-01-
 01 2009-12-01 ...

As expected date is factor, number is numeric and date2 is POSIXlt format.


  plot(tabelle$date2,tabelle$number, pch=19, xlab=date of retrieval,
  ylab=number of animals) regression.punkte-lm(tabelle$number ~
  as.Date(tabelle$date)) regression.punkte

Here you are plotting number against POSIX date2.


 
 Call:
 lm(formula = tabelle$number ~ as.Date(tabelle$date))

Here you use date probably converted by as.Date. Why you do not use 

lm(number ~ date2, data = tabelle)

which is usual form of model formula.

Try also change date2 by as.POSIXct.

Regards
Petr

 
 Coefficients:
   (Intercept)  as.Date(tabelle$date)
-24046.326  1.799
 
  abline(regression.punkte, lwd=2)
 
 
 
 
 --
 View this message in context: http://r.789695.n4.nabble.com/Problems-
 plotting-and-regression-w-r-t-date-data-type-on-x-axis-
 tp4658518p4658529.html
 Sent from the R help mailing list archive at Nabble.com.
 
 __
 R-help@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.

__
R-help@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.