An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121019/e7979a4e/attachment.pl>
Newly installed version; can't run lm function
4 messages · Michael Grant, Jorge I Velez, David Winsemius +1 more
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20121020/450af89e/attachment.pl>
On Oct 19, 2012, at 2:26 PM, Michael Grant wrote:
New installation seems to have behavior I cannot figure out. Here is illustrative sequence where I load a small data set (test) from Crawley's files and try to run a simple linear model and get an error message. Oddly, R reports that the variable 'test$ozone' is numeric while, after attaching test, the variable ozone is not numeric. Can someone please help? This behavior is occurring with multiple data sets loaded from outside R. Thank you in advance. Michael Grant Example:
test
ozone garden 1 3 A 2 5 B 3 4 A 4 5 B 5 4 A 6 6 B 7 3 A 8 7 B 9 2 A 10 4 B 11 3 A 12 4 B 13 1 A 14 3 B 15 3 A 16 5 B 17 5 A 18 6 B 19 2 A 20 5 B
is.data.frame(test)
[1] TRUE
is.numeric(test$ozone)
[1] TRUE
is.factor(test$garden)
[1] TRUE
lm(ozone~garden)
Error in model.frame.default(formula = ozone ~ garden, drop.unused.levels = TRUE) : invalid type (list) for variable 'ozone'
This is not surprising for two reasons. Crawley's presumptuously named text "The R Book" teaches students to use `attach`, leaving them unprepared to deal with the rather predictable confusion that unfortunate practice leads to. (The second reason is that you have not yet used `attach`.)
attach(test) is.numeric(ozone)
And when I do that, I do not get the same result: test <- read.table(text=" ozone garden 1 3 A 2 5 B 3 4 A 4 5 B 5 4 A 6 6 B 7 3 A 8 7 B 9 2 A 10 4 B 11 3 A 12 4 B 13 1 A 14 3 B 15 3 A 16 5 B 17 5 A 18 6 B 19 2 A 20 5 B", header=TRUE) is.data.frame(test) #[1] TRUE is.numeric(test$ozone) #[1] TRUE is.factor(test$garden) #[1] TRUE lm(ozone~garden) # Error which seems perfectly expected without a 'data' argument. attach(test) is.numeric(ozone) ####------- # I get
is.numeric(ozone)
[1] TRUE So you have done something else. What it is we cannot tell. Why not send a letter to Crawley? He's the one you paid money for that fat book, and whose dataset you are importing in some unspecified manner. Or perhaps use: str(test)
[1] FALSE
is.numeric(test$ozone)
[1] TRUE [[alternative HTML version deleted]]
David Winsemius, MD Alameda, CA, USA > sessionInfo() R version 2.15.1 (2012-06-22) Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)
You have an object named "ozone" kicking around in your workspace/global
environment. This is apparently not of mode "numeric" and it is masking
the "ozone" object (column) from the attached data frame "test".
Simply doing ls() would have told you this.
Presumably you also got a warning about this when you attached test.
Heed the warning!
As others have advised you ***don't use attach()***!!!
Instead, use "data= ..." in your call to lm().
cheers,
Rolf Turner
On 20/10/12 10:26, Michael Grant wrote:
New installation seems to have behavior I cannot figure out. Here is illustrative sequence where I load a small data set (test) from Crawley's files and try to run a simple linear model and get an error message. Oddly, R reports that the variable 'test$ozone' is numeric while, after attaching test, the variable ozone is not numeric. Can someone please help? This behavior is occurring with multiple data sets loaded from outside R. Thank you in advance. Michael Grant Example:
test
ozone garden 1 3 A 2 5 B 3 4 A 4 5 B 5 4 A 6 6 B 7 3 A 8 7 B 9 2 A 10 4 B 11 3 A 12 4 B 13 1 A 14 3 B 15 3 A 16 5 B 17 5 A 18 6 B 19 2 A 20 5 B
is.data.frame(test)
[1] TRUE
is.numeric(test$ozone)
[1] TRUE
is.factor(test$garden)
[1] TRUE
lm(ozone~garden)
Error in model.frame.default(formula = ozone ~ garden, drop.unused.levels = TRUE) : invalid type (list) for variable 'ozone'
attach(test) is.numeric(ozone)
[1] FALSE
is.numeric(test$ozone)
[1] TRUE [[alternative HTML version deleted]]
______________________________________________ R-help at 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.