[R] Different output for lm Mac vs PC

Marc Schwartz marc_schwartz at me.com
Wed Jan 15 15:25:57 CET 2014


Devin,

You should find out when and how that option was altered from the default, lest you find that virtually any modeling that you do on the Mac will be affected by that change, fundamentally altering the interpretation of the model results.

Regards,

Marc

On Jan 15, 2014, at 7:17 AM, CASENHISER, DEVIN M <devin at uthsc.edu> wrote:

> Yes that's it!
> 
> My mac has:
> 
> 	> options('contrasts')
> 	$contrasts
> 	[1] "contr.sum"  "contr.poly"
> 
> 
> whereas the PC has
> 
> 	$contrasts
> 	unordered           ordered
> 	"contr.treatment"      "contr.poly"
> 
> 
> I've changed the mac with
> 
> 	options(contrasts=c('contr.treatment','contr.poly'))
> 
> 
> and that has solved the issue.
> 
> Thanks Greg and Marc!
> 
> Cheers!
> Devin
> 
> 
> On 1/14/14 5:35 PM, "Marc Schwartz" <marc_schwartz at me.com> wrote:
> 
>> Good catch Greg.
>> 
>> The Mac output observed can result from either:
>> 
>> options(contrasts = c("contr.helmert", "contr.poly"))
>> 
>> or
>> 
>> options(contrasts = c("contr.sum", "contr.poly"))
>> 
>> being run first, before calling the model code.
>> 
>> I checked the referenced tutorial and did not see any steps pertaining to
>> altering the default contrasts. So either code along the lines of the
>> above was manually entered on the Mac at some point or perhaps there is a
>> change to the defaults on Devin's Mac system? The latter perhaps in
>> ~/.Rprofile to mimic S-PLUS' behavior, in the case of Helmert contrasts?
>> 
>> Devin, note that the model output lines for both the intercept and sex,
>> beyond the way in which 'sex' is displayed (sex1 versus sexmale), are
>> rather different and are consistent with the use of non-default contrasts
>> on the Mac, as Greg noted.
>> 
>> Regards,
>> 
>> Marc
>> 
>> 
>> On Jan 14, 2014, at 3:55 PM, Greg Snow <538280 at gmail.com> wrote:
>> 
>>> I would suggest running the code:
>>> 
>>> options('contrasts')
>>> 
>>> on both machines to see if there is a difference.  Having the default
>>> contrasts set differently would be one explanation.
>>> 
>>> On Tue, Jan 14, 2014 at 2:28 PM, Marc Schwartz <marc_schwartz at me.com>
>>> wrote:
>>>> 
>>>> On Jan 14, 2014, at 2:23 PM, CASENHISER, DEVIN M <devin at uthsc.edu>
>>>> wrote:
>>>> 
>>>>> I've noticed that I get different output when running a linear model
>>>>> on my Mac versus on my PC. Same effect, but the Mac assumes the
>>>>> predictor as a 0 level whereas the PC uses the first category
>>>>> (alphabetically).
>>>>> 
>>>>> So for example (using Bodo Winter's example from his online linear
>>>>> models tutorial):
>>>>> 
>>>>> pitch = c(233,204,242,130,112,142)
>>>>> sex=c(rep("female",3),rep("male",3))
>>>>> 
>>>>> summary(lm(pitch~sex))
>>>>> 
>>>>> My Mac, running R 3.0.2, outputs:
>>>>> 
>>>>> Residuals:
>>>>>    1       2       3       4       5       6
>>>>> 6.667 -22.333  15.667   2.000 -16.000  14.000
>>>>> 
>>>>> Coefficients:
>>>>>          Estimate Std. Error t value Pr(>|t|)
>>>>> (Intercept)  177.167      7.201  24.601 1.62e-05 ***
>>>>> sex1          49.167      7.201   6.827  0.00241 **
>>>>> ---
>>>>> Signif. codes:  0 Œ***¹ 0.001 Œ**¹ 0.01 Œ*¹ 0.05 Œ.¹ 0.1 Œ ¹ 1
>>>>> 
>>>>> Residual standard error: 17.64 on 4 degrees of freedom
>>>>> Multiple R-squared:  0.921, Adjusted R-squared:  0.9012
>>>>> F-statistic: 46.61 on 1 and 4 DF,  p-value: 0.002407
>>>>> 
>>>>> But my PC, running R 3.0.2, outputs:
>>>>> 
>>>>> Residuals:
>>>>>    1       2       3       4       5       6
>>>>> 6.667 -22.333  15.667   2.000 -16.000  14.000
>>>>> 
>>>>> Coefficients:
>>>>>          Estimate Std. Error t value Pr(>|t|)
>>>>> (Intercept)   226.33      10.18  22.224 2.43e-05 ***
>>>>> sexmale       -98.33      14.40  -6.827  0.00241 **
>>>>> ---
>>>>> Signif. codes:  0 Œ***¹ 0.001 Œ**¹ 0.01 Œ*¹ 0.05 Œ.¹ 0.1 Œ ¹ 1
>>>>> 
>>>>> Residual standard error: 17.64 on 4 degrees of freedom
>>>>> Multiple R-squared:  0.921,     Adjusted R-squared:  0.9012
>>>>> F-statistic: 46.61 on 1 and 4 DF,  p-value: 0.002407
>>>>> 
>>>>> 
>>>>> I understand that these are the same (correct) answer, but it does
>>>>> make it a little more challenging to follow examples (when learning or
>>>>> teaching) given that the coefficient outputs are calculated
>>>>> differently.
>>>>> 
>>>>> I don't suppose that there is way to easily change either output so
>>>>> that they correspond (some setting I've overlooked perhaps)?
>>>>> 
>>>>> Thanks and Cheers!
>>>>> Devin
>>>> 
>>>> 
>>>> On my Mac with R 3.0.2, I get the same output as you get on your
>>>> Windows machine.
>>>> 
>>>> Something on your Mac is amiss, resulting in the recoding of 'sex'
>>>> into a factor with presumably 0/1 levels rather than the default
>>>> textual factor levels. If you try something like:
>>>> 
>>>> model.frame(pitch ~ sex)
>>>> 
>>>> the output should give you an indication of the actual data that is
>>>> being used for your model in each case.
>>>> 
>>>> Either you have other code on your Mac that you did not include above,
>>>> which is modifying the contents of 'sex', or you have some other
>>>> behavior going on in the default workspace.
>>>> 
>>>> I would check for other objects in your current workspace on the Mac,
>>>> using ls() for example, that might be conflicting. If you are running
>>>> some type of GUI on your Mac (eg. the default R.app or perhaps
>>>> RStudio), try running R from a terminal session, using 'R --vanilla'
>>>> from the command line, to be sure that you are not loading a default
>>>> workspace containing objects that are resulting in the altered
>>>> behavior. Then re-try the example code. If that resolves the issue, you
>>>> may want to delete, or at least rename/move the .RData file contained
>>>> in your default working directory.
>>>> 
>>>> Regards,
>>>> 
>>>> Marc Schwartz
>> 




More information about the R-help mailing list