Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

QUESTION: Give the Predicted kidney weights of every combination of sex and trea

ID: 3357833 • Letter: Q

Question

QUESTION: Give the Predicted kidney weights of every combination of sex and treatment. To do this, use the predict function in R.

I copy pasted the kidney weight data below this question. It is the kidney weight data between male and female rats in response to doses of a drug and a placebo.

My predict function in R is not working. I tried this following code:

dat <- expand.grid(treatment = levels(kidney$treatment),
sex = levels(kidney$sex))
pred <- predict(kidney.add, dat)

treatment sex weight placebo m 6.62 placebo m 6.65 placebo m 5.78 placebo m 5.63 placebo m 6.05 placebo m 6.48 placebo m 5.5 placebo m 5.37 Dose 1 m 6.25 Dose 1 m 6.95 Dose 1 m 5.61 Dose 1 m 5.4 Dose 1 m 6.89 Dose 1 m 6.24 Dose 1 m 5.85 Dose 2 m 7.11 Dose 2 m 5.68 Dose 2 m 6.23 Dose 2 m 7.11 Dose 2 m 5.55 Dose 2 m 5.9 Dose 2 m 5.98 Dose 2 m 7.14 Dose 3 m 6.93 Dose 3 m 7.17 Dose 3 m 7.12 Dose 3 m 6.43 Dose 3 m 6.96 Dose 3 m 7.08 Dose 3 m 7.93 Dose 4 m 7.26 Dose 4 m 6.45 Dose 4 m 6.37 Dose 4 m 6.54 Dose 4 m 6.93 Dose 4 m 6.4 Dose 4 m 7.01 Dose 4 m 7.74 Dose 4 m 7.63 Dose 4 m 7.62 Dose 4 m 7.38 placebo f 7.11 placebo f 7.08 placebo f 5.95 placebo f 7.36 placebo f 7.58 placebo f 7.39 placebo f 8.25 placebo f 6.95 Dose 1 f 6.23 Dose 1 f 7.93 Dose 1 f 7.59 Dose 1 f 7.14 Dose 1 f 8.03 Dose 1 f 7.31 Dose 1 f 6.91 Dose 1 f 7.52 Dose 1 f 7.32 Dose 2 f 7.4 Dose 2 f 6.51 Dose 2 f 6.85 Dose 2 f 7.17 Dose 2 f 6.76 Dose 2 f 7.69 Dose 2 f 8.18 Dose 2 f 7.05 Dose 2 f 8.75 Dose 2 f 7.53 Dose 3 f 6.65 Dose 3 f 8.11 Dose 3 f 7.37 Dose 3 f 8.43 Dose 3 f 8.21 Dose 3 f 7.14 Dose 3 f 8.25 Dose 4 f 9.26 Dose 4 f 8.62 Dose 4 f 7.72 Dose 4 f 8.54 Dose 4 f 7.88 Dose 4 f 8.44 Dose 4 f 8.02 Dose 4 f 7.72 Dose 4 f 8.27 Dose 4 f 7.91 Dose 4 f 8.31

Explanation / Answer

The simplest way to set this up in R is as follows:

1) Read data in R

df <- read.csv('kidney.csv', header=T)

OUTPUT

> head(df)
treatment sex weight predicted_weight
1 placebo m 6.62 6.071767
2 placebo m 6.65 6.071767
3 placebo m 5.78 6.071767
4 placebo m 5.63 6.071767
5 placebo m 6.05 6.071767
6 placebo m 6.48 6.071767

In R you can use a simple glm() to fit the regression with categorical variables (which are factors). The regression yields this output.

R code: fit <- glm(weight ~ ., data=df)
summary(fit)

OUTPUT:

Call:
glm(formula = weight ~ ., data = df)

Deviance Residuals:
Min 1Q Median 3Q Max  
-1.30047 -0.43741 0.02408 0.34872 1.35046  

Coefficients:
Estimate Std. Error t value Pr(>|t|)   
(Intercept) 7.2935 0.1540 47.370 < 2e-16 ***
treatmentDose 2 0.1060 0.1979 0.536 0.59370   
treatmentDose 3 0.6569 0.2109 3.114 0.00256 **
treatmentDose 4 0.8813 0.1894 4.653 1.28e-05 ***
treatmentplacebo -0.1465 0.2038 -0.719 0.47418   
sexm -1.0752 0.1246 -8.632 4.66e-13 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

(Dispersion parameter for gaussian family taken to be 0.3317871)

Null deviance: 64.628 on 85 degrees of freedom
Residual deviance: 26.543 on 80 degrees of freedom
AIC: 156.96

Number of Fisher Scoring iterations: 2

As we can see, the intercept, does = 3, does=4, and sex plays a significant role in kidney weight (given by asterix)

And then if you want to calculate the predicted weights, it's as simple as:

df$predicted_weight <- predict(fit, newdata=df)
head(df,10)

treatment sex weight predicted_weight
1 placebo m 6.62 6.071767
2 placebo m 6.65 6.071767
3 placebo m 5.78 6.071767
4 placebo m 5.63 6.071767
5 placebo m 6.05 6.071767
6 placebo m 6.48 6.071767
7 placebo m 5.50 6.071767
8 placebo m 5.37 6.071767
9 Dose 1 m 6.25 6.218316
10 Dose 1 m 6.95 6.218316

This is the way you'll get predicted values for the weights.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote