Supported transformations

NOTE this stuff is broken right now, need to fix.

For convenience, you may wish to transform one or more of your covariates within the model estimation call. For example, imagine we want to natural log transform the variable dist in a linear regression:

cars <- transform(cars, distance = dist, dist.log = log(dist))
m.log <- lm(speed ~ log(distance), data = cars)
m.log2 <- lm(speed ~ dist.log, data = cars)

Then we want to find quantities of interest when the dist variable is 26 to 56 (the first to third quartile):

library(smargins)

summary(smargins(m.log, distance = c(26, 56)))
##   distance     mean        sd   median lower_2.5 upper_97.5
## 1       26 13.84199 0.4617833 13.85162  12.94072   14.74233
## 2       56 18.13741 0.5155141 18.14441  17.10916   19.12809

We could also estimate the model with dist as a polynomial. For example:

# Find range of polynomial values

m2 <- lm(speed ~ poly(distance, 2), data = cars)
summary(smargins(m2, distance = quantile(distance, seq(0, 1, .1))))
##    distance     mean        sd   median lower_2.5 upper_97.5
## 1       2.0 12.99141 0.4973046 12.99788  12.00160   13.96686
## 2      15.8 13.99183 0.4366190 13.99682  13.13463   14.83580
## 3      21.6 14.37630 0.4258560 14.37970  13.52668   15.19530
## 4      26.0 14.65377 0.4220096 14.65653  13.82173   15.46188
## 5      32.0 15.01237 0.4211610 15.01364  14.18265   15.82197
## 6      36.0 15.23878 0.4225430 15.23867  14.39817   16.05637
## 7      46.0 15.76047 0.4292674 15.75571  14.91550   16.58397
## 8      54.0 16.13224 0.4351293 16.13012  15.27046   16.97216
## 9      64.4 16.55495 0.4407401 16.55512  15.67824   17.41067
## 10     80.4 17.07156 0.4456192 17.07674  16.18547   17.92683
## 11    120.0 17.65311 0.5548389 17.65892  16.55362   18.71160

Splines and other transformations should generally work as expected.

library(splines)

m3 <- lm(speed ~ bs(distance, df = 4), data = cars)
summary(smargins(m3, distance = seq(min(distance), max(distance), 10)))
##    distance      mean        sd    median  lower_2.5 upper_97.5
## 1         2  4.738110 2.1007892  4.712074  0.6612385   8.935140
## 2        12  7.334477 1.0585096  7.339090  5.2777960   9.397163
## 3        22 10.113624 0.6741707 10.120130  8.7670558  11.423903
## 4        32 12.874144 0.5274330 12.877663 11.8221971  13.905404
## 5        42 15.414629 0.5931427 15.417901 14.2500052  16.576044
## 6        52 17.533674 0.7341443 17.546929 16.0663042  18.953153
## 7        62 19.040774 0.7364010 19.053327 17.5840248  20.459963
## 8        72 19.996204 0.8859907 20.000110 18.2797111  21.705790
## 9        82 20.711011 1.2512117 20.707253 18.2404908  23.150062
## 10       92 21.507152 1.4442417 21.482937 18.6482456  24.337453
## 11      102 22.706577 1.5399503 22.694490 19.7192689  25.775006
## 12      112 24.631242 2.8411004 24.567075 19.2890227  30.238632