# Predicting the difference between two groups in R

I'm working on practice exercises with R. From the data, I now have to determine the difference in predicted male compared to female spending while keeping all other predictors constant.

I replaced the sex 0,1 with male, female:

> spending$sex=factor(spending$sex)
> levels(spending$sex)=c("male", "female") > tail(spending) sex status income verbal gamble 42 male 61 15.00 9 69.7 43 male 75 3.00 8 13.3 44 male 66 3.25 9 0.6 45 male 62 4.94 6 38.0 46 male 71 1.50 7 14.4 47 male 71 2.50 9 19.2 > summary(spending) sex status income verbal male :28 Min. :18.00 Min. : 0.600 Min. : 1.00 female:19 1st Qu.:28.00 1st Qu.: 2.000 1st Qu.: 6.00 Median :43.00 Median : 3.250 Median : 7.00 Mean :45.23 Mean : 4.642 Mean : 6.66 3rd Qu.:61.50 3rd Qu.: 6.210 3rd Qu.: 8.00 Max. :75.00 Max. :15.000 Max. :10.00 gamble Min. : 0.0 1st Qu.: 1.1 Median : 6.0 Mean : 19.3 3rd Qu.: 19.4 Max. :156.0 Now, this is where I got stumped, how do I set this up with constants and only use the sex variable? • Related to your question Predicting response for two groups in multiple regression (under a different account). Could you please indicate to one of the moderators (by flagging your post here) which account you want to keep and register? – chl Sep 11 '12 at 19:50 • the other question can be removed..Chrissy was working on the same problem and posted same data. – MsSnowy Sep 11 '12 at 23:13 ## 1 Answer You've got one big problem in your data as posted: there's no "spending" column, so you don't have a response. Once you get that, just run a linear model with sex as your only predictor. yourModel <- lm(spending ~ sex, data = spending) summary(yourModel) The base level (intercept only) corresponds to your factor's 0-level, in this case male. The difference between the levels is sex coefficient in the model output. When you have more than 2 levels, the output is explicit. For example, using the built-in dataset mtcars head(mtcars) mtcars$cyl <- factor(mtcars$cyl) mylm <- lm(mpg ~ cyl, data = mtcars) > levels(mtcars$cyl)
[1] "4" "6" "8"

> summary(mylm)

Call:
lm(formula = mpg ~ cyl, data = mtcars)

Residuals:
Min      1Q  Median      3Q     Max
-5.2636 -1.8357  0.0286  1.3893  7.2364

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)  26.6636     0.9718  27.437  < 2e-16 ***
cyl6         -6.9208     1.5583  -4.441 0.000119 ***
cyl8        -11.5636     1.2986  -8.905 8.57e-10 ***
---

Here the factor levels are called out, with the 0-level (4-cylinder) corresponding the the intercept. As in your example, when there are no other predictors, this is as simple as taking the means for each factor level. The 4-cyl mean is 26.66, same as the intercept, and then adding -6.9 we get the 6-cyl mean, and adding -11.56 (to the original 26.66) we get the 8-cyl mean of 15.1.

> with(mtcars, tapply(mpg, cyl, mean)) ## just calculates mean mpg at each level of cyl
4        6        8
26.66364 19.74286 15.10000
• sorry, i had two data sets on board..gamble is the spending column....I copied the wrong text. I did as you suggested, – MsSnowy Sep 11 '12 at 15:58
• I hit enter to quickly Residuals: Min 1Q Median 3Q Max -29.775 -18.325 -3.766 6.334 126.225 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 29.775 5.498 5.415 2.28e-06 *** sex -25.909 8.648 -2.996 0.00444 ** --- Signif. codes: 0 ‘’ 0.001 ‘’ 0.01 ‘’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 29.09 on 45 degrees of freedom Multiple R-squared: 0.1663, Adjusted R-squared: 0.1478 F-statistic: 8.977 on 1 and 45 DF, p-value: 0.004437 But how do i predict male compared to female from this lm? – MsSnowy Sep 11 '12 at 16:20
• This means that females spends on average 25.909 less than males – O_Devinyak Sep 11 '12 at 16:37
• But, how is it females spend less and not vice-versa? – MsSnowy Sep 11 '12 at 17:09
• Despite your recoding of sex as a factor, you could still think of it as being 0 or 1. Since you put "male" as first, that's the "0", the baseline. The sex coefficient with value -25.909 is in effect only for females. Since you have no other predictors, you should also be able to enter in with(spending, tapply(gamble, sex, mean)) and see that 25.909 is simply the difference between the spending means for males and females. – Gregor Thomas Sep 11 '12 at 17:23