Search code examples
rglm

Inline square transformation giving different results than transformed column values


When using a square transformation inline (i.e. using ~ Parameter^2 in the model formula) is giving different results than when I transform the Parameter first into a different column and use that column in the model ( i.e Parameter2 = Parameter^2 and then using ~ Parameter2 in the formula)

Worst.Perimeter2 = Worst.Perimeter^2

m0x_ <- glm(formula = Diagnosis ~ Worst.Perimeter2, family = binomial, 
    data = cancerdata)
summary(m0x_)

results:
Coefficients:
                   Estimate Std. Error z value Pr(>|z|)    
(Intercept)      -1.090e+01  1.132e+00  -9.634   <2e-16 ***
Worst.Perimeter2  8.974e-04  9.792e-05   9.165   <2e-16 ***


m0x1_ <- glm(formula = Diagnosis ~ Worst.Perimeter^2, family = binomial, 
    data = cancerdata)
summary(m0x1_)

Results:
Coefficients:
                 Estimate Std. Error z value Pr(>|z|)    
(Intercept)     -20.81003    2.22116  -9.369   <2e-16 ***
Worst.Perimeter   0.18931    0.02068   9.156   <2e-16 ***


I would expect them to give the same results for the coefficients but clearly that's not the case. How do I interpret these different results?


Solution

  • ^ is a formula operator. You can use it to specify a formula that includes interactions as in this example:

    # Formula `(a + b)^2` means `1 + a + b + a*b`
    head(model.matrix(~ (mpg + wt)^2, data = mtcars))
    #>                   (Intercept)  mpg    wt mpg:wt
    #> Mazda RX4                   1 21.0 2.620 55.020
    #> Mazda RX4 Wag               1 21.0 2.875 60.375
    #> Datsun 710                  1 22.8 2.320 52.896
    #> Hornet 4 Drive              1 21.4 3.215 68.801
    #> Hornet Sportabout           1 18.7 3.440 64.328
    #> Valiant                     1 18.1 3.460 62.626
    

    If you want ^ to be interpreted as an arithmetic operator instead, use the I (AsIs) operator as in this example:

    head(model.matrix(~ I((mpg + wt)^2), data = mtcars))
    #>                   (Intercept) I((mpg + wt)^2)
    #> Mazda RX4                   1        557.9044
    #> Mazda RX4 Wag               1        570.0156
    #> Datsun 710                  1        631.0144
    #> Hornet 4 Drive              1        605.8982
    #> Hornet Sportabout           1        490.1796
    #> Valiant                     1        464.8336
    

    Created on 2019-04-06 by the reprex package (v0.2.1)