Amherst-Statistics / IS5inR

Companion materials for De Veaux, Velleman, and Bock's "Intro Stats" 5th edition
MIT License
0 stars 3 forks source link

Error in Linear Model #20

Closed mchien20 closed 6 years ago

mchien20 commented 6 years ago

library(mosaic) library(readr) DirtBikes <- read_csv("http://nhorton.people.amherst.edu/is5/data/Dirt_bikes_2014.csv") DirtBikes <- DirtBikes %>% filter(Cooling != "NA") %>% mutate(Cooling = ifelse(Cooling == "Air-Cooled", "Air-Cooled", "LiquidCooled")) gf_point(MSRP ~ (Displacement)^(1/3), color = ~ Cooling, data = DirtBikes) %>% gf_lm() bikeslm <- lm(MSRP ~ Displacement^(1/3) + Cooling, data = DirtBikes)

The last line of code gives me an error: Error in terms.formula(formula, data = data) : invalid power in formula I understand that it's because of Displacement^(1/3), but I'm not sure how to get around it. (Chapter 9, page 291)

nicholasjhorton commented 6 years ago

Try enclosing that term with the I() function. See ?I (Capital i) for details and examples.

All the best,

Nick

On Jul 3, 2018, at 11:38 AM, Margaret Chien notifications@github.com wrote:

library(mosaic) library(readr) DirtBikes <- read_csv("http://nhorton.people.amherst.edu/is5/data/Dirt_bikes_2014.csv") DirtBikes <- DirtBikes %>% filter(Cooling != "NA") %>% mutate(Cooling = ifelse(Cooling == "Air-Cooled", "Air-Cooled", "LiquidCooled")) gf_point(MSRP ~ (Displacement)^(1/3), color = ~ Cooling, data = DirtBikes) %>% gf_lm() bikeslm <- lm(MSRP ~ Displacement^(1/3) + Cooling, data = DirtBikes)

The last line of code gives me an error: Error in terms.formula(formula, data = data) : invalid power in formula I understand that it's because of Displacement^(1/3), but I'm not sure how to get around it.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

nicholasjhorton commented 6 years ago

I’m worried about the filter() command. Are these really coded as strings “NA”? I hope not. Using !is.na() would be far preferable if they are coded as NA.

All the best,

Nick

On Jul 3, 2018, at 11:38 AM, Margaret Chien notifications@github.com wrote:

library(mosaic) library(readr) DirtBikes <- read_csv("http://nhorton.people.amherst.edu/is5/data/Dirt_bikes_2014.csv") DirtBikes <- DirtBikes %>% filter(Cooling != "NA") %>% mutate(Cooling = ifelse(Cooling == "Air-Cooled", "Air-Cooled", "LiquidCooled")) gf_point(MSRP ~ (Displacement)^(1/3), color = ~ Cooling, data = DirtBikes) %>% gf_lm() bikeslm <- lm(MSRP ~ Displacement^(1/3) + Cooling, data = DirtBikes)

The last line of code gives me an error: Error in terms.formula(formula, data = data) : invalid power in formula I understand that it's because of Displacement^(1/3), but I'm not sure how to get around it.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

nicholasjhorton commented 6 years ago

Another option would be to generate the transformed variable in the mutate call. That might be my preference as the name would automatically show up in the plot.

All the best,

Nick

On Jul 3, 2018, at 11:38 AM, Margaret Chien notifications@github.com wrote:

library(mosaic) library(readr) DirtBikes <- read_csv("http://nhorton.people.amherst.edu/is5/data/Dirt_bikes_2014.csv") DirtBikes <- DirtBikes %>% filter(Cooling != "NA") %>% mutate(Cooling = ifelse(Cooling == "Air-Cooled", "Air-Cooled", "LiquidCooled")) gf_point(MSRP ~ (Displacement)^(1/3), color = ~ Cooling, data = DirtBikes) %>% gf_lm() bikeslm <- lm(MSRP ~ Displacement^(1/3) + Cooling, data = DirtBikes)

The last line of code gives me an error: Error in terms.formula(formula, data = data) : invalid power in formula I understand that it's because of Displacement^(1/3), but I'm not sure how to get around it.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.