
Dayvon B.
asked 02/12/24The Orange data frame from datasets...
Using the dataset Orange from R can help solve this question.
Part c)
According to the model, what is the estimate of the mean of circumference 𝑌 for a tree of age 𝑥 = 900. That is, find 𝜇̂ 𝑌|𝑥=900= estimate of 𝐸[𝑌|𝑥=900]? Do NOT round your answer! Hint: Use the command
predict(mod, data.frame(age=??))
for appropriately chosen replacement of the symbol ??
Write your answer as
ans = <something here>
Part d)
Find 92% confidence interval for 𝐸[𝑌|𝑥=900]. You should use command of the format
predict(mod, data.frame(age=??), data=Orange, interval="confidence", level=??)
and replace the two ??
simbols with appropriate values.
Create variables CIlwr
and CIupr
with lower and upper end points of the desired interval. Do NOT round your answers.
My code for part(c):
```
x <- 900
predicted_mean <- predict(mod, newdata = data.frame(age = x))
x
```
My code for part(d):
```
x <- 900
# Use predict() to calculate the confidence interval
confidence_interval <- predict(mod, newdata = data.frame(age = x), data = Orange, interval = "confidence", level = 0.92)
# Extract the lower and upper bounds of the confidence interval
CIlwr <- confidence_interval[1]
CIupr <- confidence_interval[2]
# Print the confidence interval
CIlwr
CIupr
```
My code does not pass certain test. Feel free to correct my code with the appropriate one.
Your code my pass ALL test put forward by the following:
Part (c)
```
## check whether variable ans exists and is numeric
if (test_that(desc="", code={
expect_equal(exists("ans", mode="numeric"), TRUE)
}) != TRUE) stop("variable ans not created or is not numeric!")
```
And
(I received an error for this test:
)
```
## check whether the 6th decimal digit is 2
if (test_that(desc="", code={
expect_equal(floor(as.numeric(ans)*10^6) %% 10 == 2, TRUE)
}) != TRUE) stop("variable ans not created or is not numeric!")
```
Part (D):
```
## check whether variables CIlwr and CIupr exist and are numeric
if (test_that(desc="", code={
expect_equal(exists("CIlwr", mode="numeric"), TRUE)
}) != TRUE) stop("variable CIlwr is not created or not numeric!")
if (test_that(desc="", code={
expect_equal(exists("CIupr", mode="numeric"), TRUE)
}) != TRUE) stop("variable CIupr is not created or not numeric!")
```
And
)
```
## check whether the 8th decimal digit of CIlwr is correct
if (test_that(desc="Sorry, wrong answer.", code={
expect_equal(floor(as.numeric(CIlwr)*10^8) %% 10 == 8, TRUE)
}) != TRUE) stop("Sorry, wrong answer.")
## check whether the 8th decimal digit of CIupr is correct
if (test_that(desc="Sorry, wrong answer.", code={
expect_equal(floor(as.numeric(CIupr)*10^8) %% 10 == 1, TRUE)
}) != TRUE) stop("Sorry, wrong answer.")
```
1 Expert Answer
Derrick B. answered 09/03/24
Master of Science, Applied Mathematics; 23 Yrs of Tutoring Experience
For part (c), it may just be that you didn’t save your answer as “ans”. Instead, you saved it as “predicted_mean”. The rest of your part (c) code looks correct, and the answer (saved as “ans”) on my computer passed the tests.
For part (d), run this line of code:
confidence_interval
When you run that, you will see that the confidence interval that R gives you from the predict() function is a vector of 3 elements, not 2! Therefore, you will see that the lower limit of the confidence interval isn’t “confidence_interval[1]”. It’s actually “confidence_interval[2]”! And similarly, the upper limit is not “confidence_interval[2]”; it is “confidence_interval[3]”.
Feel free to book a session with me for more help in R!
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Michael H.
08/20/24