[R] lm

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Sun Feb 21 07:29:55 CET 2016


Your first predict sets up a newdata with a column name that is not the same as the one that you used in the lm formula. The failure to match causes R to look in the global environment, where it finds AT.

You really should make a habit of always putting your regression data into a data.frame and specifying that using the data argument to lm. Working with some input data in a data frame and other data not in the same data frame can make it much harder to debug and to understand. 

B <- data.frame( Time = timevalues )
B$Counts  <- predict( lm( Counts ~ Time, data = A ), newdata = B )

-- 
Sent from my phone. Please excuse my brevity.

On February 20, 2016 5:27:52 PM PST, Yuan Jian via R-help <r-help at r-project.org> wrote:
>Hello,I used l  to draw a figure, but I got different result (26 vs
>301) when I input the same parameters.
>
>> length(predict(lm(A$Counts ~ AT),list(ATE=timevalues)))
>[1] 26
>> length(predict(lm(A$Counts ~ ATE),list(ATE=timevalues)))
>[1] 301
>
>all variables are initialized as below:
>>A <- structure(list(Time = c(0, 1, 2, 4, 6, 8, 9, 10, 11, 12, 13, 
> 14, 15, 16, 18, 19, 20, 21, 22, 24, 25, 26, 27, 28, 29, 30), 
> Counts = c(126.6, 101.8, 71.6, 101.6, 68.1, 62.9, 45.5, 41.9, 
> 46.3, 34.1, 38.2, 41.7, 24.7, 41.5, 36.6, 19.6, 
> 22.8, 29.6, 23.5, 15.3, 13.4, 26.8, 9.8, 18.8, 25.9, 19.3)), .Names =
>c("Time", "Counts"),
> row.names = c(1L, 2L, 3L, 5L, 7L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
>16L, 17L, 19L, 20L, 21L, 22L, 23L, 25L, 26L, 27L, 28L, 29L, 30L, 31L),
> class = "data.frame")
>>timevalues <- seq(0, 30, 0.1)
>>AT<-A$Time>ATE<-A$Time
>	[[alternative HTML version deleted]]
>
>______________________________________________
>R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.

	[[alternative HTML version deleted]]



More information about the R-help mailing list