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)
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.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.