Question

Help with coding in R: cyl<-factor(scan(text= "6 6 4 6 8 6 8 4 4 6...

Help with coding in R:

cyl<-factor(scan(text=
"6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4"))


am<-factor(scan(text=
"1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1"))

## 1)## Using the data `cyl` and `am` (transmission type) from Part II, group vehicles based into 8 cylinder and less than 8 cyl. Test whether there is evidence of association between many cylinders and automatic transmissions. (_Hint:_ use `levels()` to re-level `cyl` and then use `chisq.test()`).
Every time I run the following code, I get an error:
levels(cyl) <-c("8 cyl","less than 8")
Error in `levels<-.factor`(`*tmp*`, value = c("8 cyl", "less than 8")) : 
  number of levels differs

## 2)## The built in dataset `faithful` records the time between eruptions and the length of the prior eruption for 272 inter-eruption intervals (load the data with `data(faithful)`). Examine the distribution of each of these variables with `stem()` or `hist()`. Plot these variables against each other with the length of each eruption (`eruptions`) on the x axis. How would you describe the relationship? 

## 3)## Fit a regression of `waiting` as a function of `eruptions`. What can we say about this regression? Compare the distribution of the residuals (`model$resid` where `model` is your lm object) to the distribution of the variables.

## 4)## Is this data well suited to regression? Create a categorical variable from `eruptions` to separate long eruptions from short eruptions (2 groups) and fit a model (ANOVA) of `waiting` based on this. (_Hint:_ use `cut()` to make the categorical variable, and `lm()` to fit the model). How did you choose the point at which to cut the data? How might changing the cutpoint change the results?
0 0
Add a comment Improve this question Transcribed image text
Answer #1

You are defining the new levels in the wrong way. Because, when you run only levels(cyl), you will get

> levels(cyl)
[1] "4" "6" "8"

In this first and second level is "4" and "6" which are less than 8 and the third level is "8" which is equal to 8.

Therefore, assign these levels in the sequence in which they are i.e for level "4" and "6", assign level "less than 8" two times in same order and then assign "8 cyl" as the third level as shown below:

levels(cyl) <- c("less than 8", "less than 8", "8 cyl");

The Output of the whole program is given below:

> cyl<-factor(scan(text="6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4"))
Read 32 items
>
>
> am<-factor(scan(text="1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1"))
Read 32 items
> levels(cyl) <- c("less than 8", "less than 8", "8 cyl");
> levels(cyl)
[1] "less than 8" "8 cyl"    
> cyl
[1] less than 8 less than 8 less than 8 less than 8 8 cyl       less than 8
[7] 8 cyl       less than 8 less than 8 less than 8 less than 8 8 cyl    
[13] 8 cyl       8 cyl       8 cyl       8 cyl       8 cyl       less than 8
[19] less than 8 less than 8 less than 8 8 cyl       8 cyl       8 cyl    
[25] 8 cyl       less than 8 less than 8 less than 8 8 cyl       less than 8
[31] 8 cyl       less than 8
Levels: less than 8 8 cyl

Add a comment
Know the answer?
Add Answer to:
Help with coding in R: cyl<-factor(scan(text= "6 6 4 6 8 6 8 4 4 6...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT