[R] Problem with by statement for spaghetti plots

dadrivr dadrivr at gmail.com
Sun Sep 4 00:24:40 CEST 2011


Hi,

I am trying to apply the example at the bottom of the following page to my
own data:
http://128.97.141.26/stat/R/faq/spagplot.htm
http://128.97.141.26/stat/R/faq/spagplot.htm 

The code from the example is:
/tolerance<-read.table("http://www.ats.ucla.edu/stat/R/faq/tolpp.csv",sep=",",
header=T)
fit <- by(tolerance, tolerance$id,function(x) fitted.values(lm(tolerance ~
time, data=x)))  
fit1 <- unlist(fit)
names(fit1) <- NULL
interaction.plot(tolerance$age, tolerance$id, fit1,xlab="time",
ylab="tolerance", legend=F)/

Here is my code:
/mydata <-
read.table("https://www.sugarsync.com/pf/D000507_6529035_6683114",
header=TRUE)
fit <- by(mydata, mydata$id, function(x) fitted.values(lm(outcome ~ age,
data=x)))
fit1 <- unlist(fit)
names(fit1) <- NULL
interaction.plot(mydata$age, mydata$id, fit1,legend=F)/

I get the following error:
fit <- by(mydata, mydata$id, function(x) fitted.values(lm(outcome ~ age,
data=x)))
"/Error in lm.fit(x, y, offset = offset, singular.ok = singular.ok, ...) : 
  0 (non-NA) cases/"

The error suggests that there is an error with the lm statement.  I think it
may have to do with missing values.  Nonetheless, I don't get an error with
the following statements:
fitted.values(lm(outcome ~ age, data=mydata))
lm(outcome ~ age, data=mydata)

If I specify 'data = mydata' instead of 'data = x', there is no error, but
the result is not expected (compared to the example code):
fit <- by(mydata, mydata$id, function(x) fitted.values(lm(outcome ~ age,
data=mydata)))

I'm expecting each id value to have 3 fitted values representing each of the
three ages.  That's not what I get, however --- I get hundreds of fitted
values per id value, which is unexpected.

I have my data in tall format, just like the example.  Any ideas?  Any help
would be greatly appreciated.  Thanks!


--
View this message in context: http://r.789695.n4.nabble.com/Problem-with-by-statement-for-spaghetti-plots-tp3788536p3788536.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list