diff --git a/docs/CONTRIBUTING.html b/docs/CONTRIBUTING.html index 072f893..637ec50 100644 --- a/docs/CONTRIBUTING.html +++ b/docs/CONTRIBUTING.html @@ -25,7 +25,12 @@ + + + + + @@ -48,8 +53,12 @@ - margins + + margins + 0.3.23 + + @@ -96,7 +106,7 @@ -
+
+ + diff --git a/docs/ISSUE_TEMPLATE.html b/docs/ISSUE_TEMPLATE.html index 048f8fd..e830a39 100644 --- a/docs/ISSUE_TEMPLATE.html +++ b/docs/ISSUE_TEMPLATE.html @@ -25,7 +25,12 @@ + + + + + @@ -48,8 +53,12 @@ - margins + + margins + 0.3.23 +
+
@@ -96,7 +106,7 @@ -
+
+ + diff --git a/docs/LICENSE-text.html b/docs/LICENSE-text.html index a0876b0..18eebe3 100644 --- a/docs/LICENSE-text.html +++ b/docs/LICENSE-text.html @@ -25,7 +25,12 @@ + + + + + @@ -48,8 +53,12 @@ - margins + + margins + 0.3.23 +
+
@@ -96,7 +106,7 @@ -
+
+ + diff --git a/docs/LICENSE.html b/docs/LICENSE.html index b0d81df..e0b8866 100644 --- a/docs/LICENSE.html +++ b/docs/LICENSE.html @@ -25,7 +25,12 @@ + + + + + @@ -48,8 +53,12 @@ - margins + + margins + 0.3.23 +
+
@@ -96,7 +106,7 @@ -
+
+ + diff --git a/docs/PULL_REQUEST_TEMPLATE.html b/docs/PULL_REQUEST_TEMPLATE.html index f0a3e02..69386d7 100644 --- a/docs/PULL_REQUEST_TEMPLATE.html +++ b/docs/PULL_REQUEST_TEMPLATE.html @@ -25,7 +25,12 @@ + + + + + @@ -48,8 +53,12 @@ - margins + + margins + 0.3.23 +
+
@@ -96,7 +106,7 @@ -
+
+ + diff --git a/docs/articles/Introduction.html b/docs/articles/Introduction.html index 7e6b0af..cbd7618 100644 --- a/docs/articles/Introduction.html +++ b/docs/articles/Introduction.html @@ -18,7 +18,7 @@ -
+
-
+
-

margins is an effort to port Stata’s (closed source) margins command to R as an S3 generic method for calculating the marginal effects (or “partial effects”) of covariates included in model objects (like those of classes “lm” and “glm”). A plot method for the new “margins” class additionally ports the marginsplot command, and various additional functions support interpretation and visualization of such models.

Stata’s margins command is very simple and intuitive to use:

. import delimited mtcars.csv
@@ -121,7 +125,7 @@ 

2018-03-18

## wt -3.1198 0.6613 -4.7176 0.0000 -4.4160 -1.8236

With the exception of differences in rounding, the above results match identically what Stata’s margins command produces. Using the plot() method also yields an aesthetically similar result to Stata’s marginsplot:

plot(m)
-

+

Using Optional Arguments in margins @@ -137,7 +141,7 @@

The at argument has been translated into margins() in a very similar manner. It can be used by specifying a list of variable names and specified values for those variables at which to calculate marginal effects, such as margins(x, at = list(hp=150)). When using at, margins() constructs modified datasets - using build_datalist() - containing the specified values and calculates marginal effects on each modified dataset, rbind-ing them back into a single “margins” object.

Stata’s atmeans argument is not implemented in margins() for various reasons, including because it is possible to achieve the effect manually through an operation like data$var <- mean(data$var, na.rm = TRUE) and passing the modified data frame to margins(x, data = data).

-

At present, margins() does not implement the over option. The reason for this is also simple: R already makes data subsetting operations quite simple using simple [ extraction. If, for example, one wanted to calculate marginal effects on subsets of a data frame, those subsets can be passed directly to margins() via the data argument (as in a call to prediction()).

+

At present, margins() does not implement the over option. The reason for this is also simple: R already makes data subsetting operations quite simple using simple [ extraction. If, for example, one wanted to calculate marginal effects on subsets of a data frame, those subsets can be passed directly to margins() via the data argument (as in a call to prediction()).

The rest of this vignette shows how to use at and data to obtain various kinds of marginal effects, and how to use plotting functions to visualize those inferences.

@@ -157,7 +161,7 @@

summary(margins(x, variables = "hp"))
##  factor     AME     SE       z      p   lower   upper
 ##      hp -0.0253 0.0105 -2.4046 0.0162 -0.0459 -0.0047
-

In an ordinary least squares regression, there is really only one way of examining marginal effects (that is, on the scale of the outcome variable). In a generalized linear model (e.g., logit), however, it is possible to examine true “marginal effects” (i.e., the marginal contribution of each variable on the scale of the linear predictor) or “partial effects” (i.e., the contribution of each variable on the outcome scale, conditional on the other variables involved in the link function transformation of the linear predictor). The latter are the default in margins(), which implicitly sets the argument margins(x, type = "response") and passes that through to prediction() methods. To obtain the former, simply set margins(x, type = "link"). There’s some debate about which of these is preferred and even what to call the two different quantities of interest. Regardless of all of that, here’s how you obtain either:

+

In an ordinary least squares regression, there is really only one way of examining marginal effects (that is, on the scale of the outcome variable). In a generalized linear model (e.g., logit), however, it is possible to examine true “marginal effects” (i.e., the marginal contribution of each variable on the scale of the linear predictor) or “partial effects” (i.e., the contribution of each variable on the outcome scale, conditional on the other variables involved in the link function transformation of the linear predictor). The latter are the default in margins(), which implicitly sets the argument margins(x, type = "response") and passes that through to prediction() methods. To obtain the former, simply set margins(x, type = "link"). There’s some debate about which of these is preferred and even what to call the two different quantities of interest. Regardless of all of that, here’s how you obtain either:

x <- glm(am ~ cyl + hp * wt, data = mtcars, family = binomial)
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
margins(x, type = "response")
@@ -234,9 +238,30 @@

## 5.424 -0.6764

The marginal effects in the first column of results reveal that the average marginal effect of wt is large and negative except when wt is very large, in which case it has an effect not distinguishable from zero. We can easily plot these results using the cplot() function to see the effect visually in terms of either predicted fuel economy or the marginal effect of wt:

cplot(x, "wt", what = "prediction", main = "Predicted Fuel Economy, Given Weight")
-

+
##       xvals    yvals    upper    lower
+## 1  1.513000 32.36718 35.03500 29.69936
+## 2  1.675958 30.79531 33.07715 28.51348
+## 3  1.838917 29.28565 31.23015 27.34115
+## 4  2.001875 27.83818 29.49769 26.17868
+## 5  2.164833 26.45291 27.88384 25.02199
+## 6  2.327792 25.12984 26.39172 23.86796
+## 7  2.490750 23.86897 25.02106 22.71687
+## 8  2.653708 22.67029 23.76564 21.57494
+## 9  2.816667 21.53381 22.61340 20.45422
+## 10 2.979625 20.45953 21.54975 19.36930
+## 11 3.142583 19.44744 20.56171 18.33318
+## 12 3.305542 18.49755 19.64011 17.35500
+## 13 3.468500 17.60986 18.78010 16.43963
+## 14 3.631458 16.78437 17.98078 15.58796
+## 15 3.794417 16.02108 17.24475 14.79740
+## 16 3.957375 15.31998 16.57774 14.06221
+## 17 4.120333 14.68108 15.98801 13.37414
+## 18 4.283292 14.10437 15.48533 12.72342
+## 19 4.446250 13.58987 15.07937 12.10037
+## 20 4.609208 13.13756 14.77796 11.49716
+

cplot(x, "wt", what = "effect", main = "Average Marginal Effect of Weight")
-

+

A really nice feature of Stata’s margins command is that it handles factor variables gracefully. This functionality is difficult to emulate in R, but the margins() function does its best. Here we see the marginal effects of a simple regression that includes a factor variable:

x <- lm(mpg ~ factor(cyl) * hp + wt, data = mtcars)
 margins(x)
@@ -297,9 +322,51 @@

For example, we can use cplot() to quickly display the predicted fuel economy of a vehicle from a model:

x <- lm(mpg ~ cyl + wt * am, data = mtcars)
 cplot(x, "cyl")
-

+

cplot(x, "wt")
-

+

+
##       xvals    yvals    upper    lower
+## 1  4.000000 21.30004 23.47379 19.12629
+## 2  4.166667 21.10314 23.17171 19.03458
+## 3  4.333333 20.90625 22.87186 18.94064
+## 4  4.500000 20.70936 22.57461 18.84410
+## 5  4.666667 20.51246 22.28039 18.74454
+## 6  4.833333 20.31557 21.98973 18.64141
+## 7  5.000000 20.11867 21.70327 18.53408
+## 8  5.166667 19.92178 21.42175 18.42180
+## 9  5.333333 19.72488 21.14607 18.30369
+## 10 5.500000 19.52799 20.87725 18.17873
+## 11 5.666667 19.33110 20.61643 18.04576
+## 12 5.833333 19.13420 20.36487 17.90354
+## 13 6.000000 18.93731 20.12384 17.75078
+## 14 6.166667 18.74041 19.89455 17.58628
+## 15 6.333333 18.54352 19.67801 17.40902
+## 16 6.500000 18.34662 19.47489 17.21836
+## 17 6.666667 18.14973 19.28541 17.01405
+## 18 6.833333 17.95284 19.10930 16.79637
+## 19 7.000000 17.75594 18.94587 16.56602
+## 20 7.166667 17.55905 18.79408 16.32402
+##       xvals    yvals    upper    lower
+## 1  1.513000 25.65915 28.41557 22.90273
+## 2  1.675958 24.99524 27.50747 22.48300
+## 3  1.838917 24.33132 26.60508 22.05756
+## 4  2.001875 23.66741 25.71041 21.62440
+## 5  2.164833 23.00349 24.82639 21.18059
+## 6  2.327792 22.33958 23.95737 20.72178
+## 7  2.490750 21.67566 23.10981 20.24152
+## 8  2.653708 21.01175 22.29295 19.73054
+## 9  2.816667 20.34783 21.51891 19.17676
+## 10 2.979625 19.68392 20.80041 18.56743
+## 11 3.142583 19.02000 20.14557 17.89444
+## 12 3.305542 18.35609 19.55294 17.15924
+## 13 3.468500 17.69217 19.01248 16.37187
+## 14 3.631458 17.02826 18.51122 15.54530
+## 15 3.794417 16.36435 18.03777 14.69092
+## 16 3.957375 15.70043 17.58371 13.81715
+## 17 4.120333 15.03652 17.14326 12.92977
+## 18 4.283292 14.37260 16.71251 12.03269
+## 19 4.446250 13.70869 16.28884 11.12853
+## 20 4.609208 13.04477 15.87044 10.21910

The slopes of the predicted value lines are the marginal effect of wt when am == 0 and am == 1. We can obtain these slopes using margins() and specifying the at argument:

margins(x, at = list(am = 0:1))
## Average marginal effects at specified values
@@ -309,13 +376,13 @@

## 1 -1.181 -6.566 -1.566

Another plotting function - the persp() method for “lm” objects - gives even more functionality:

persp(x, "cyl", "wt")
-

+

This three-dimensional representation can be easily rotated using the theta and phi arguments. If multiple values are specified for each, facetting is automatically provided:

persp(x, "cyl", "wt", theta = c(0, 90))
-

+

Because these three-dimensional representations can be challenging to interpret given the distortion of displaying a three-dimensional surface in two dimensions, the image() method provides a “flat” alternative to convey the same information using the same semantics:

image(x, "cyl", "wt")
-

+


@@ -421,7 +488,7 @@

## -0.03051 -4.132

On average across the cases in the dataset, the effect of horsepower is slightly negative. On average, the effect of weight is also negative. Both decrease fuel economy. But what is the marginal effect of each variable across the range of values we actually observe in the data. To get a handle on this, we can use the persp() method provided by margins.

persp(x2, "wt", "hp", theta = c(45, 135, 225, 315), what = "effect")
-

+

To make sense of this set of plots (actually, the same plot seen from four different angles), it will also be helpful to have the original regression results close at-hand:

summary(x2)
## 
@@ -448,7 +515,7 @@ 

For example, if we take the partial derivative of the regression equation with respect to wt (i.e., the marginal effect of weight), the equation is: d_mpg/d_wt = (-8.22) + (0.03 * hp). This means that the marginal effect of weight is large and negative when horsepower is zero (which never occurs in the mtcars dataset) and decreases in magnitude and becoming more positive as horsepower increases. We can see this in the above graph that the marginal effect of weight is constant across levels of weight because wt does not enter into the partial derivative. Across levels, of horsepower, however, the marginal effect becomes more positive. This is clear looking at the “front” or “back” edges of the surface, which are straight-linear increases. The slope of those edges is 0.03 (the coefficient on the interaction term).

If we then take the partial derivative with respect to hp (to obtain the marginal effect of horsepower), the equation is: d_mpg/d_hp = (-0.12) + (0.03 * wt). When wt is zero, this partial derivative (or marginal effect) is -0.12 miles/gallon. The observed range of wt, however, is only: 1.513, 5.424. We can see these results in the analogous graph of the marginal effects of horsepower (below). The “front” and “back” edges of the graph are now flat (reflecting how the marginal effect of horsepower is constant across levels of horsepower), while the “front-left” and “right-back” edges of the surface are lines with slope 0.03, reflecting the coefficient on the interaction term.

persp(x2, "hp", "wt", theta = c(45, 135, 225, 315), what = "effect")
-

+

An alternative way of plotting these results is to take “slices” of the three-dimensional surface and present them in a two-dimensional graph, similar to what we did above with the indicator-by-continuous approach. That strategy would be especially appropriate for a categorical-by-continuous interaction where the categories of the first variable did not necessarily have a logical ordering sufficient to draw a three-dimensional surface.

@@ -456,8 +523,29 @@

ggplot2 examples

It should be noted that cplot() returns a fairly tidy data frmae, making it possible to use ggplot2 as an alternative plotting package to display the kinds of relationships of typical interest. For example, returning to our earlier example:

x1 <- lm(mpg ~ drat * wt * am, data = mtcars)
-cdat <- cplot(x1, "wt", draw = FALSE)
-head(cdat)
+cdat <- cplot(x1, "wt", draw = FALSE)
+
##       xvals    yvals    upper     lower
+## 1  1.513000 28.66765 33.04903 24.286273
+## 2  1.675958 27.71126 31.71243 23.710103
+## 3  1.838917 26.75488 30.38319 23.126563
+## 4  2.001875 25.79849 29.06385 22.533126
+## 5  2.164833 24.84210 27.75810 21.926095
+## 6  2.327792 23.88571 26.47147 21.299955
+## 7  2.490750 22.92932 25.21225 20.646397
+## 8  2.653708 21.97293 23.99282 19.953051
+## 9  2.816667 21.01655 22.83057 19.202525
+## 10 2.979625 20.06016 21.74656 18.373748
+## 11 3.142583 19.10377 20.75901 17.448522
+## 12 3.305542 18.14738 19.87315 16.421613
+## 13 3.468500 17.19099 19.07759 15.304391
+## 14 3.631458 16.23460 18.35187 14.117337
+## 15 3.794417 15.27821 17.67591 12.880521
+## 16 3.957375 14.32183 17.03432 11.609334
+## 17 4.120333 13.36544 16.41648 10.314396
+## 18 4.283292 12.40905 15.81532  9.002781
+## 19 4.446250 11.45266 15.22612  7.679197
+## 20 4.609208 10.49627 14.64572  6.346819
+
head(cdat)
##      xvals    yvals    upper    lower
 ## 1 1.513000 28.66765 33.04903 24.28627
 ## 2 1.675958 27.71126 31.71243 23.71010
@@ -534,14 +622,35 @@ 

## 0.009283 0.01064

These marginal effects reflect, on average across all of the cases in our data, how much more likely a woman is to have diabetes. Because this is an instantaneous effect, it can be a little hard to conceptualize. I find it helpful to take a look at a predicted probability plot to understand what is going on. Let’s take a look, for example, at the effect of age on the probability of having diabetes:

cplot(g1, "age")
-

+
##    xvals     yvals     upper     lower
+## 1   21.0 0.1884779 0.2489698 0.1279860
+## 2   23.5 0.2121299 0.2710552 0.1532047
+## 3   26.0 0.2378800 0.2950225 0.1807374
+## 4   28.5 0.2657016 0.3214139 0.2099893
+## 5   31.0 0.2955155 0.3509421 0.2400889
+## 6   33.5 0.3271841 0.3843232 0.2700450
+## 7   36.0 0.3605098 0.4219464 0.2990731
+## 8   38.5 0.3952358 0.4636015 0.3268701
+## 9   41.0 0.4310522 0.5085084 0.3535959
+## 10  43.5 0.4676046 0.5555658 0.3796435
+## 11  46.0 0.5045082 0.6035841 0.4054323
+## 12  48.5 0.5413627 0.6514177 0.4313076
+## 13  51.0 0.5777701 0.6980299 0.4575102
+## 14  53.5 0.6133515 0.7425297 0.4841733
+## 15  56.0 0.6477626 0.7841952 0.5113299
+## 16  58.5 0.6807053 0.8224871 0.5389236
+## 17  61.0 0.7119373 0.8570511 0.5668234
+## 18  63.5 0.7412754 0.8877090 0.5948417
+## 19  66.0 0.7685967 0.9144398 0.6227536
+## 20  68.5 0.7938353 0.9373541 0.6503165
+

The above graph shows that as age increase, the probability of having diabetes increases. When a woman is 20 years old, the probability is about .20 whereas when a woman is 80, the probability is about 0.80. In essence, the marginal effect of age reported above is the slope of this predicted probability plot at the mean age of women in the dataset (which is 31.3162651). Clearly, this quantity is useful (it’s the impact of age for the average-aged woman) but the logit curve shows that the marginal effect of age differs considerably across ages and, as we know from above with linear models, also depends on the other variable in the interaction (skin thickness). To really understand what is going on, we need to graph the data. Let’s look at the perspective plot like the one we drew for the OLS model, above:

persp(g1, theta = c(45, 135, 225, 315), what = "prediction")
-

+

This graph is much more complicated than the analogous graph for an OLS model, this is because of the conversion between the log-odds scale of the linear predictors and the distribution of the data. What we can see is that the average marginal effect of age (across all of the women in our dataset) is highest when a woman is middle age and skin thickness is low. In these conditions, the marginal change in the probability of diabetes is about 3%, but the AME of age is nearly zero at higher and lower ages. Indeed, as skin thickness increases, the marginal effect of age flattens out and actually becomes negative (such that increasing age actually decreases the probability of diabetes for women with thick arms).

Now let’s examine the average marginal effects of skin thickness across levels of age and skin thickness:

persp(g1, theta = c(45, 135, 225, 315), what = "effect")
-

+

This graphs is somewhat flatter, indicating that the average marginal effects of skin thickness vary less than those for age. Yet, the AME of skin thickness is as high as 2% when age is low and skin thickness is high. Interestingly, however, this effect actually becomes negative as age increases. The marginal effect of skin thickness is radically different for young and old women.

For either of these cases, it would also be appropriate to draw two-dimensional plots of “slides” of these surfaces and incorporate measures of uncertainty. It is extremely difficult to convey standard errors or confidence intervals in a three-dimensional plot, so those could be quite valuable.

It is also worth comparing the above graphs to those that would result from a model without the interaction term between age and skin. It looks quite different (note, however, that it is also drawn from a higher perspective to better see the shape of the surface):

@@ -560,7 +669,6 @@

Norton, Edward C., Hua Wang, and Chunrong Ai. 2004. “Computing interaction effects and standard errors in logit and probit models.” The Stata Journal 4(2): 154-167.

Stata Corporation. “margins”. Software Documentation. Available from: http://www.stata.com/manuals13/rmargins.pdf.

Williams, Richard. 2012. “Using the margins command to estimate and interpret adjusted predictions and marginal effects.” Stata Journal 12: 308-331.

-

@@ -591,5 +699,7 @@

+ + diff --git a/docs/articles/Introduction_files/figure-html/marginsplot-1.png b/docs/articles/Introduction_files/figure-html/marginsplot-1.png index 23f8d9f..b2394c1 100644 Binary files a/docs/articles/Introduction_files/figure-html/marginsplot-1.png and b/docs/articles/Introduction_files/figure-html/marginsplot-1.png differ diff --git a/docs/articles/Introduction_files/figure-html/unnamed-chunk-11-1.png b/docs/articles/Introduction_files/figure-html/unnamed-chunk-11-1.png index 690f301..50ddfc2 100644 Binary files a/docs/articles/Introduction_files/figure-html/unnamed-chunk-11-1.png and b/docs/articles/Introduction_files/figure-html/unnamed-chunk-11-1.png differ diff --git a/docs/articles/Introduction_files/figure-html/unnamed-chunk-11-2.png b/docs/articles/Introduction_files/figure-html/unnamed-chunk-11-2.png index 1d7e0a4..77a7754 100644 Binary files a/docs/articles/Introduction_files/figure-html/unnamed-chunk-11-2.png and b/docs/articles/Introduction_files/figure-html/unnamed-chunk-11-2.png differ diff --git a/docs/articles/Introduction_files/figure-html/unnamed-chunk-15-1.png b/docs/articles/Introduction_files/figure-html/unnamed-chunk-15-1.png index 71dd30f..1377c66 100644 Binary files a/docs/articles/Introduction_files/figure-html/unnamed-chunk-15-1.png and b/docs/articles/Introduction_files/figure-html/unnamed-chunk-15-1.png differ diff --git a/docs/articles/Introduction_files/figure-html/unnamed-chunk-15-2.png b/docs/articles/Introduction_files/figure-html/unnamed-chunk-15-2.png index 1282020..e57ed20 100644 Binary files a/docs/articles/Introduction_files/figure-html/unnamed-chunk-15-2.png and b/docs/articles/Introduction_files/figure-html/unnamed-chunk-15-2.png differ diff --git a/docs/articles/Introduction_files/figure-html/unnamed-chunk-17-1.png b/docs/articles/Introduction_files/figure-html/unnamed-chunk-17-1.png index 7d337f7..8d57493 100644 Binary files a/docs/articles/Introduction_files/figure-html/unnamed-chunk-17-1.png and b/docs/articles/Introduction_files/figure-html/unnamed-chunk-17-1.png differ diff --git a/docs/articles/Introduction_files/figure-html/unnamed-chunk-18-1.png b/docs/articles/Introduction_files/figure-html/unnamed-chunk-18-1.png index 71f9831..a45ffd3 100644 Binary files a/docs/articles/Introduction_files/figure-html/unnamed-chunk-18-1.png and b/docs/articles/Introduction_files/figure-html/unnamed-chunk-18-1.png differ diff --git a/docs/articles/Introduction_files/figure-html/unnamed-chunk-19-1.png b/docs/articles/Introduction_files/figure-html/unnamed-chunk-19-1.png index 022e920..d769e8d 100644 Binary files a/docs/articles/Introduction_files/figure-html/unnamed-chunk-19-1.png and b/docs/articles/Introduction_files/figure-html/unnamed-chunk-19-1.png differ diff --git a/docs/articles/Introduction_files/figure-html/unnamed-chunk-24-1.png b/docs/articles/Introduction_files/figure-html/unnamed-chunk-24-1.png index 67a6332..8e49baf 100644 Binary files a/docs/articles/Introduction_files/figure-html/unnamed-chunk-24-1.png and b/docs/articles/Introduction_files/figure-html/unnamed-chunk-24-1.png differ diff --git a/docs/articles/Introduction_files/figure-html/unnamed-chunk-25-1.png b/docs/articles/Introduction_files/figure-html/unnamed-chunk-25-1.png index c25b71a..079d8c7 100644 Binary files a/docs/articles/Introduction_files/figure-html/unnamed-chunk-25-1.png and b/docs/articles/Introduction_files/figure-html/unnamed-chunk-25-1.png differ diff --git a/docs/articles/Introduction_files/figure-html/unnamed-chunk-27-1.png b/docs/articles/Introduction_files/figure-html/unnamed-chunk-27-1.png index 99bd79d..3367ddb 100644 Binary files a/docs/articles/Introduction_files/figure-html/unnamed-chunk-27-1.png and b/docs/articles/Introduction_files/figure-html/unnamed-chunk-27-1.png differ diff --git a/docs/articles/Introduction_files/figure-html/unnamed-chunk-29-1.png b/docs/articles/Introduction_files/figure-html/unnamed-chunk-29-1.png index e254219..8f3b111 100644 Binary files a/docs/articles/Introduction_files/figure-html/unnamed-chunk-29-1.png and b/docs/articles/Introduction_files/figure-html/unnamed-chunk-29-1.png differ diff --git a/docs/articles/Introduction_files/figure-html/unnamed-chunk-36-1.png b/docs/articles/Introduction_files/figure-html/unnamed-chunk-36-1.png index deced7c..4ac6fd5 100644 Binary files a/docs/articles/Introduction_files/figure-html/unnamed-chunk-36-1.png and b/docs/articles/Introduction_files/figure-html/unnamed-chunk-36-1.png differ diff --git a/docs/articles/Introduction_files/figure-html/unnamed-chunk-37-1.png b/docs/articles/Introduction_files/figure-html/unnamed-chunk-37-1.png index 1860f73..b222212 100644 Binary files a/docs/articles/Introduction_files/figure-html/unnamed-chunk-37-1.png and b/docs/articles/Introduction_files/figure-html/unnamed-chunk-37-1.png differ diff --git a/docs/articles/Introduction_files/figure-html/unnamed-chunk-38-1.png b/docs/articles/Introduction_files/figure-html/unnamed-chunk-38-1.png index f286fed..56695fc 100644 Binary files a/docs/articles/Introduction_files/figure-html/unnamed-chunk-38-1.png and b/docs/articles/Introduction_files/figure-html/unnamed-chunk-38-1.png differ diff --git a/docs/articles/Stata.html b/docs/articles/Stata.html index f9512c8..699b836 100644 --- a/docs/articles/Stata.html +++ b/docs/articles/Stata.html @@ -18,7 +18,7 @@ -
+
-
+
-

margins is intended as a port of (some of) the features of Stata’s margins command. This vignette compares output from Stata’s margins command for linear models against the output of margins.

library("margins")
 options(width = 100)
@@ -561,7 +565,6 @@

## hp 0.0176 0.0067 2.6189 0.0088 0.0044 0.0308 ## wt 0.2075 0.4860 0.4271 0.6693 -0.7449 1.1600

-
@@ -598,5 +601,7 @@

+ + diff --git a/docs/articles/index.html b/docs/articles/index.html index e0766ba..dd9b634 100644 --- a/docs/articles/index.html +++ b/docs/articles/index.html @@ -25,7 +25,12 @@ + + + + + @@ -38,7 +43,7 @@ -
+
@@ -96,12 +106,12 @@ - -
-
+
+ +

All vignettes

@@ -126,6 +136,8 @@

All vignettes

+ + diff --git a/docs/authors.html b/docs/authors.html index 4eaf8a5..d8156ff 100644 --- a/docs/authors.html +++ b/docs/authors.html @@ -25,7 +25,12 @@ + + + + + @@ -48,8 +53,12 @@ - margins + + margins + 0.3.23 +
+
@@ -96,22 +106,21 @@ -
+
-

Leeper TJ (2018). margins: Marginal Effects for Model Objects. -R package version 0.3.20. +R package version 0.3.23.

@Manual{,
   title = {margins: Marginal Effects for Model Objects},
   author = {Thomas J. Leeper},
   year = {2018},
-  note = {R package version 0.3.20},
+  note = {R package version 0.3.23},
 }
+ + diff --git a/docs/docsearch.css b/docs/docsearch.css new file mode 100644 index 0000000..c524034 --- /dev/null +++ b/docs/docsearch.css @@ -0,0 +1,145 @@ +/* Docsearch -------------------------------------------------------------- */ +/* + Source: https://github.com/algolia/docsearch/ + License: MIT +*/ + +.algolia-autocomplete { + display: block; + -webkit-box-flex: 1; + -ms-flex: 1; + flex: 1 +} + +.algolia-autocomplete .ds-dropdown-menu { + width: 100%; + min-width: none; + max-width: none; + padding: .75rem 0; + background-color: #fff; + background-clip: padding-box; + border: 1px solid rgba(0, 0, 0, .1); + box-shadow: 0 .5rem 1rem rgba(0, 0, 0, .175); +} + +@media (min-width:768px) { + .algolia-autocomplete .ds-dropdown-menu { + width: 175% + } +} + +.algolia-autocomplete .ds-dropdown-menu::before { + display: none +} + +.algolia-autocomplete .ds-dropdown-menu [class^=ds-dataset-] { + padding: 0; + background-color: rgb(255,255,255); + border: 0; + max-height: 80vh; +} + +.algolia-autocomplete .ds-dropdown-menu .ds-suggestions { + margin-top: 0 +} + +.algolia-autocomplete .algolia-docsearch-suggestion { + padding: 0; + overflow: visible +} + +.algolia-autocomplete .algolia-docsearch-suggestion--category-header { + padding: .125rem 1rem; + margin-top: 0; + font-size: 1.3em; + font-weight: 500; + color: #00008B; + border-bottom: 0 +} + +.algolia-autocomplete .algolia-docsearch-suggestion--wrapper { + float: none; + padding-top: 0 +} + +.algolia-autocomplete .algolia-docsearch-suggestion--subcategory-column { + float: none; + width: auto; + padding: 0; + text-align: left +} + +.algolia-autocomplete .algolia-docsearch-suggestion--content { + float: none; + width: auto; + padding: 0 +} + +.algolia-autocomplete .algolia-docsearch-suggestion--content::before { + display: none +} + +.algolia-autocomplete .ds-suggestion:not(:first-child) .algolia-docsearch-suggestion--category-header { + padding-top: .75rem; + margin-top: .75rem; + border-top: 1px solid rgba(0, 0, 0, .1) +} + +.algolia-autocomplete .ds-suggestion .algolia-docsearch-suggestion--subcategory-column { + display: block; + padding: .1rem 1rem; + margin-bottom: 0.1; + font-size: 1.0em; + font-weight: 400 + /* display: none */ +} + +.algolia-autocomplete .algolia-docsearch-suggestion--title { + display: block; + padding: .25rem 1rem; + margin-bottom: 0; + font-size: 0.9em; + font-weight: 400 +} + +.algolia-autocomplete .algolia-docsearch-suggestion--text { + padding: 0 1rem .5rem; + margin-top: -.25rem; + font-size: 0.8em; + font-weight: 400; + line-height: 1.25 +} + +.algolia-autocomplete .algolia-docsearch-footer { + float: none; + width: auto; + height: auto; + padding: .75rem 1rem 0; + font-size: .95rem; + line-height: 1; + color: #767676; + background-color: rgb(255, 255, 255); + border-top: 1px solid rgba(0, 0, 0, .1) +} + +.algolia-autocomplete .algolia-docsearch-footer--logo { + display: inline; + overflow: visible; + color: inherit; + text-indent: 0; + background: 0 0 +} + +.algolia-autocomplete .algolia-docsearch-suggestion--highlight { + color: #FF8C00; + background: rgba(232, 189, 54, 0.1) +} + + +.algolia-autocomplete .algolia-docsearch-suggestion--text .algolia-docsearch-suggestion--highlight { + box-shadow: inset 0 -2px 0 0 rgba(105, 105, 105, .5) +} + +.algolia-autocomplete .ds-suggestion.ds-cursor .algolia-docsearch-suggestion--content { + background-color: rgba(192, 192, 192, .15) +} diff --git a/docs/index.html b/docs/index.html index 1b7ad90..660cfdd 100644 --- a/docs/index.html +++ b/docs/index.html @@ -19,7 +19,7 @@ -
+
-
+
-
@@ -175,12 +178,12 @@

## treatment 0.0432 0.0147 2.9320 0.0034 0.0143 0.0720
# margins, dydx(treatment) at(age=(20(10)60))
 summary(margins(mod2, at = list(age = c(20, 30, 40, 50, 60)), variables = "treatment"))
-
##     factor age     AME     SE       z      p   lower  upper
-##  treatment  20 -0.0009 0.0043 -0.2061 0.8367 -0.0093 0.0075
-##  treatment  30  0.0034 0.0107  0.3199 0.7490 -0.0176 0.0245
-##  treatment  40  0.0301 0.0170  1.7736 0.0761 -0.0032 0.0634
-##  treatment  50  0.0990 0.0217  4.5666 0.0000  0.0565 0.1415
-##  treatment  60  0.1896 0.0384  4.9341 0.0000  0.1143 0.2649
+
##     factor     age     AME     SE       z      p   lower  upper
+##  treatment 20.0000 -0.0009 0.0043 -0.2061 0.8367 -0.0093 0.0075
+##  treatment 30.0000  0.0034 0.0107  0.3199 0.7490 -0.0176 0.0245
+##  treatment 40.0000  0.0301 0.0170  1.7736 0.0761 -0.0032 0.0634
+##  treatment 50.0000  0.0990 0.0217  4.5666 0.0000  0.0565 0.1415
+##  treatment 60.0000  0.1896 0.0384  4.9341 0.0000  0.1143 0.2649

This functionality removes the need to modify data before performing such calculations, which can be quite unwieldy when many specifications are desired.

If one desires subgroup effects, simply pass a subset of data to the data argument:

# effects for men
@@ -216,17 +219,42 @@ 

## bmi 0.0261 0.0009 28.4995 0.0000 0.0243 0.0279 ## sex -0.0911 0.0085 -10.7063 0.0000 -0.1077 -0.0744

plot(marg3)
-

+

In addition to the estimation procedures and plot() generic, margins offers several plotting methods for model objects. First, there is a new generic cplot() that displays predictions or marginal effects (from an “lm” or “glm” model) of a variable conditional across values of third variable (or itself). For example, here is a graph of predicted probabilities from a logit model:

mod4 <- glm(am ~ wt*drat, data = mtcars, family = binomial)
 cplot(mod4, x = "wt", se.type = "shade")
-

+
##       xvals       yvals      upper       lower
+## 1  1.513000 0.927274748 1.25767803  0.59687146
+## 2  1.675958 0.896156250 1.31282164  0.47949086
+## 3  1.838917 0.853821492 1.36083558  0.34680740
+## 4  2.001875 0.798115859 1.38729030  0.20894142
+## 5  2.164833 0.727945940 1.37431347  0.08157841
+## 6  2.327792 0.644257693 1.30643930 -0.01792391
+## 7  2.490750 0.550714595 1.17940279 -0.07797360
+## 8  2.653708 0.453441410 1.00638808 -0.09950526
+## 9  2.816667 0.359598025 0.81514131 -0.09594526
+## 10 2.979625 0.275390447 0.63577343 -0.08499254
+## 11 3.142583 0.204601856 0.48756886 -0.07836515
+## 12 3.305542 0.148285654 0.37415646 -0.07758515
+## 13 3.468500 0.105415989 0.28892829 -0.07809631
+## 14 3.631458 0.073865178 0.22356331 -0.07583296
+## 15 3.794417 0.051216829 0.17224934 -0.06981569
+## 16 3.957375 0.035248556 0.13162443 -0.06112732
+## 17 4.120333 0.024132208 0.09961556 -0.05135115
+## 18 4.283292 0.016461806 0.07467832 -0.04175471
+## 19 4.446250 0.011201450 0.05550126 -0.03309836
+## 20 4.609208 0.007609032 0.04093572 -0.02571766
+

And fitted values with a factor independent variable:

cplot(lm(Sepal.Length ~ Species, data = iris))
-

+
##        xvals yvals   upper   lower
+## 1     setosa 5.006 5.14869 4.86331
+## 2 versicolor 5.936 6.07869 5.79331
+## 3  virginica 6.588 6.73069 6.44531
+

and a graph of the effect of drat across levels of wt:

cplot(mod4, x = "wt", dx = "drat", what = "effect", se.type = "shade")
-

+

cplot() also returns a data frame of values, so that it can be used just for calculating quantities of interest before plotting them with another graphics package, such as ggplot2:

library("ggplot2")
 dat <- cplot(mod4, x = "wt", dx = "drat", what = "effect", draw = FALSE)
@@ -245,16 +273,16 @@ 

ylab("Average Marginal Effect of Rear Axle Ratio") + ggtitle("Predicting Automatic/Manual Transmission from Vehicle Characteristics") + theme_bw()

-

+

Second, the package implements methods for “lm” and “glm” class objects for the persp() generic plotting function. This enables three-dimensional representations of predicted outcomes:

persp(mod1, xvar = "cyl", yvar = "hp")
-

+

and marginal effects:

persp(mod1, xvar = "cyl", yvar = "hp", what = "effect", nx = 10)
-

+

And if three-dimensional plots aren’t your thing, there are also analogous methods for the image() generic, to produce heatmap-style representations:

image(mod1, xvar = "cyl", yvar = "hp", main = "Predicted Fuel Efficiency,\nby Cylinders and Horsepower")
-

+

The numerous package vignettes and help files contain extensive documentation and examples of all package functionality.

@@ -264,12 +292,12 @@

library("microbenchmark")
 microbenchmark(marginal_effects(mod1))
## Unit: milliseconds
-##                    expr      min       lq     mean   median       uq      max neval
-##  marginal_effects(mod1) 3.366046 3.461649 3.915458 3.619783 4.001432 7.468022   100
+## expr min lq mean median uq max neval +## marginal_effects(mod1) 4.131673 4.643902 5.675486 5.256868 6.20093 11.5074 100
microbenchmark(margins(mod1))
## Unit: milliseconds
-##           expr      min      lq     mean   median       uq      max neval
-##  margins(mod1) 24.42826 26.2162 32.09026 29.94945 31.86568 166.3731   100
+## expr min lq mean median uq max neval +## margins(mod1) 32.62725 42.80285 60.27534 58.37503 74.78273 123.3838 100

The most computationally expensive part of margins() is variance estimation. If you don’t need variances, use marginal_effects() directly or specify margins(..., vce = "none").

@@ -277,10 +305,10 @@

Requirements and Installation

CRAN DownloadsBuild Status Build status codecov.io Project Status: Active - The project has reached a stable, usable state and is being actively developed.

-

The development version of this package can be installed directly from GitHub using ghit:

-
if (!require("ghit")) {
-    install.packages("ghit")
-    library("ghit")
+

The development version of this package can be installed directly from GitHub using remotes:

+
if (!require("remotes")) {
+    install.packages("remotes")
+    library("remotes")
 }
 install_github("leeper/prediction")
 install_github("leeper/margins")
@@ -288,34 +316,44 @@ 

# building vignettes takes a moment, so for a quicker install set: install_github("leeper/margins", build_vignettes = FALSE)

-
+
@@ -330,5 +368,7 @@

Developers

+ + diff --git a/docs/index_files/figure-html/cplot1-1.png b/docs/index_files/figure-html/cplot1-1.png index 16ac86e..ab424f3 100644 Binary files a/docs/index_files/figure-html/cplot1-1.png and b/docs/index_files/figure-html/cplot1-1.png differ diff --git a/docs/index_files/figure-html/cplot3-1.png b/docs/index_files/figure-html/cplot3-1.png index 534cebe..ed85527 100644 Binary files a/docs/index_files/figure-html/cplot3-1.png and b/docs/index_files/figure-html/cplot3-1.png differ diff --git a/docs/jquery.sticky-kit.min.js b/docs/jquery.sticky-kit.min.js index e2a3c6d..1c16271 100644 --- a/docs/jquery.sticky-kit.min.js +++ b/docs/jquery.sticky-kit.min.js @@ -1,5 +1,7 @@ +/* Sticky-kit v1.1.2 | WTFPL | Leaf Corcoran 2015 | */ /* - Sticky-kit v1.1.2 | WTFPL | Leaf Corcoran 2015 | http://leafo.net + Source: https://github.com/leafo/sticky-kit + License: MIT */ (function(){var b,f;b=this.jQuery||window.jQuery;f=b(window);b.fn.stick_in_parent=function(d){var A,w,J,n,B,K,p,q,k,E,t;null==d&&(d={});t=d.sticky_class;B=d.inner_scrolling;E=d.recalc_every;k=d.parent;q=d.offset_top;p=d.spacer;w=d.bottoming;null==q&&(q=0);null==k&&(k=void 0);null==B&&(B=!0);null==t&&(t="is_stuck");A=b(document);null==w&&(w=!0);J=function(a,d,n,C,F,u,r,G){var v,H,m,D,I,c,g,x,y,z,h,l;if(!a.data("sticky_kit")){a.data("sticky_kit",!0);I=A.height();g=a.parent();null!=k&&(g=g.closest(k)); if(!g.length)throw"failed to find stick parent";v=m=!1;(h=null!=p?p&&a.closest(p):b("
"))&&h.css("position",a.css("position"));x=function(){var c,f,e;if(!G&&(I=A.height(),c=parseInt(g.css("border-top-width"),10),f=parseInt(g.css("padding-top"),10),d=parseInt(g.css("padding-bottom"),10),n=g.offset().top+c+f,C=g.height(),m&&(v=m=!1,null==p&&(a.insertAfter(h),h.detach()),a.css({position:"",top:"",width:"",bottom:""}).removeClass(t),e=!0),F=a.offset().top-(parseInt(a.css("margin-top"),10)||0)-q, diff --git a/docs/news/index.html b/docs/news/index.html index 1f0cd14..ee24647 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -25,7 +25,12 @@ + + + + + @@ -48,8 +53,12 @@ - margins + + margins + 0.3.23 +
+
@@ -96,26 +106,58 @@ -
- -
+
+
-
-
+

-margins 0.3.18 Unreleased +margins 0.3.23 Unreleased +

+
    +
  • Fix a small issue in print() and summary() methods related to the release of prediction 0.3.6.
  • +
+
+
+

+margins 0.3.22 Unreleased +

+
    +
  • Expanded support for objects of class “merMod” from lme4, including support for variance estimation and an expanded test suite. (#56)
  • +
+
+
+

+margins 0.3.21 Unreleased +

+
    +
  • Modified the internals of gradient_factory() to be more robust to an expanded set of model classes through the introduction of an internal function reset_coefs(). A test suite for this function has been added.
  • +
+
+
+

+margins 0.3.20 2018-04-24 +

+
    +
  • Added support for objects of class “ivreg” from AER.
  • +
  • +margins.default() now attempts to calculate marginal effect variances in order to, by default, support additional model classes.
  • +
+
+
+

+margins 0.3.19 Unreleased

  • Added support for objects of class “betareg” from betareg. (#90)
-
+

-margins 0.3.18 Unreleased +margins 0.3.18 Unreleased

  • @@ -231,7 +273,7 @@

    margins 0.3.6 Unreleased

@@ -255,7 +297,7 @@

margins 0.3.3 Unreleased

    -
  • Fixed “margins” object structure in margins.merMod().
  • +
  • Fixed “margins” object structure in margins.merMod().
  • Switched print() and summary() methods to using weighted.mean() instead of mean(). (#45)
@@ -291,7 +333,7 @@

  • plot.margins() now displays marginal effects across each level of at. (#58)
  • -build_margins() and thus margins() no longer returns the original data twice (a bug introduced by change in behavior of prediction()). (#57)
  • +build_margins() and thus margins() no longer returns the original data twice (a bug introduced by change in behavior of prediction()). (#57)
  • All methods for objects of class "marginslist" have been removed. (#58)
  • The at argument in plot.margins() has been renamed to pos, to avoid ambiguity with at as used elsewhere in the package.
  • @@ -335,7 +377,7 @@

    • The extract_marginal_effects() function has been removed and replaced by marginal_effects() methods for objects of classes "margins" and "marginslist".
    • -
    • Added a dependency on prediction v.0.1.3 and, implicitly, an enhances suggestion of survey v3.31-5 to resolve an underlying prediction() issue for models of class "svyglm". (#47, h/t Carl Ganz)
    • +
    • Added a dependency on prediction v.0.1.3 and, implicitly, an enhances suggestion of survey v3.31-5 to resolve an underlying prediction() issue for models of class "svyglm". (#47, h/t Carl Ganz)
  • @@ -362,7 +404,7 @@

    margins 0.2.20 Unreleased

      -
    • Factored the prediction() generic and methods into a separate package, prediction, to ease maintainence.
    • +
    • Factored the prediction() generic and methods into a separate package, prediction, to ease maintainence.
    • Added a print.summary.margins() method to separate construction of the summary data frame the printing thereof.
    • The “Technical Details” vignette now describes the package functionality and computational approach in near-complete detail.
    @@ -392,7 +434,7 @@

    • Fixes to handling of factors and ordered variables converted within formulae. (#38)
    • -
    • Reconfigured the data argument in margins() and prediction() to be clearer about what is happening when it is set to missing.
    • +
    • Reconfigured the data argument in margins() and prediction() to be clearer about what is happening when it is set to missing.
    @@ -401,7 +443,7 @@

    • Switched to using a more reliable “central difference” numerical differentiation and updated the calculation of the step size to follow marfx (#31, h/t Jeffrey Arnold)
    • -
    • Added some functionality prediction() methods to, hopefully, reduce memory footprint of model objects. (#26)
    • +
    • Added some functionality prediction() methods to, hopefully, reduce memory footprint of model objects. (#26)
    • Changed the capitalization of the variances field in “margins” objects (to lower case), for consistency.
    • Fixed some small errors in documentation and improved width of examples.
    @@ -436,7 +478,7 @@

    margins 0.2.11 Unreleased

      -
    • Added a prediction() method for “gls” objects (from MASS::gls()). (#3)
    • +
    • Added a prediction() method for “gls” objects (from MASS::gls()). (#3)
    @@ -452,8 +494,8 @@

    margins 0.2.8 Unreleased

    @@ -461,8 +503,8 @@

    margins 0.2.7 Unreleased

    @@ -471,11 +513,11 @@

    • -marginal_effects() and prediction() are now S3 generics, with methods for “lm” and “glm” objects, improving extensability. (#39, #40)
    • +marginal_effects() and prediction() are now S3 generics, with methods for “lm” and “glm” objects, improving extensability. (#39, #40)
    • -prediction() returns a new class (“prediction”) and gains a print() method.
    • -
    • Added preliminary support for “loess” objects, including methods for prediction(), marginal_effects(), cplot(), and persp(). No effect variances are currently calculated. (#3)
    • -
    • Added a prediction() method for “nls” objects. (#3)
    • +prediction() returns a new class (“prediction”) and gains a print() method. +
    • Added preliminary support for “loess” objects, including methods for prediction(), marginal_effects(), cplot(), and persp(). No effect variances are currently calculated. (#3)
    • +
    • Added a prediction() method for “nls” objects. (#3)
    • Internal function get_effect_variances() gains a “none” option for the vce argument, to skip calculation of ME variances.
    @@ -566,15 +608,18 @@

  • Initial package released.
  • -
    +
    @@ -99,16 +109,19 @@ -
    +
    +

    Xenophobic Vote Share in 2003 Russian Duma Elections

    +
    alexseev
    @@ -157,6 +170,8 @@

    Contents

    + + diff --git a/docs/reference/cplot.html b/docs/reference/cplot.html index 66c9836..27264bc 100644 --- a/docs/reference/cplot.html +++ b/docs/reference/cplot.html @@ -25,10 +25,15 @@ + + + + + @@ -51,8 +56,12 @@ - margins + + margins + 0.3.23 +
    +
    @@ -99,16 +109,19 @@ -
    +
    +

    Draw one or more conditioanl effects plots reflecting predictions or marginal effects from a model, conditional on a covariate. Currently methods exist for “lm”, “glm”, “loess” class models.

    +
    cplot(object, ...)
     
    @@ -211,7 +224,7 @@ 

    Conditional predicted value and average marginal effect plots for models

    scatter.col = factor.col, scatter.bg = scatter.col, scatter.cex = 0.5, rug = TRUE, rug.col = col, rug.size = -0.02, ...)
    -

    Arguments

    +

    Arguments

    @@ -391,7 +404,7 @@

    Details

    See also

    -

    plot.margins, persp.lm

    +

    Examples

    @@ -499,6 +512,8 @@

    Contents

    + + diff --git a/docs/reference/dydx.html b/docs/reference/dydx.html index 5469f8e..da8081f 100644 --- a/docs/reference/dydx.html +++ b/docs/reference/dydx.html @@ -25,10 +25,15 @@ + + + + + @@ -51,8 +56,12 @@ - margins + + margins + 0.3.23 + + @@ -99,16 +109,19 @@ -
    +
    +

    Differentiate an Estimated Model Function with Respect to One Variable, or calculate a discrete difference (“first difference”) as appropriate.

    +
    dydx(data, model, variable, ...)
     
    @@ -129,7 +142,7 @@ 

    Marginal Effect of a Given Variable

    dydx(data, model, variable, type = c("response", "link"), as.data.frame = TRUE, ...)
    -

    Arguments

    +

    Arguments

    @@ -192,60 +205,24 @@

    R

    See also

    -

    marginal_effects, margins

    +

    Examples

    require("datasets") x <- lm(mpg ~ cyl * hp + wt, data = head(mtcars)) # marginal effect (numerical derivative) -dydx(head(mtcars), x, "hp")
    #> dydx_hp -#> 1 0.6845469 -#> 2 0.6845469 -#> 3 1.0942088 -#> 4 0.6845469 -#> 5 0.2748850 -#> 6 0.6845469
    +dydx(head(mtcars), x, "hp")
    #> Error in prediction(model = model, data = data.table::rbindlist(list(d0, d1)), type = type, calculate_se = FALSE, ...): could not find function "prediction"
    # other discrete differences ## change from min(mtcars$hp) to max(mtcars$hp) -dydx(head(mtcars), x, "hp", change = "minmax")
    #> dydx_hp -#> 1 56.13285 -#> 2 56.13285 -#> 3 89.72512 -#> 4 56.13285 -#> 5 22.54057 -#> 6 56.13285
    ## change from 1st quartile to 3rd quartile -dydx(head(mtcars), x, "hp", change = "iqr")
    #> dydx_hp -#> 1 3.422734 -#> 2 3.422734 -#> 3 5.471044 -#> 4 3.422734 -#> 5 1.374425 -#> 6 3.422734
    ## change from mean(mtcars$hp) +/- sd(mtcars$hp) -dydx(head(mtcars), x, "hp", change = "sd")
    #> dydx_hp -#> 1 39.82549 -#> 2 39.82549 -#> 3 63.65876 -#> 4 39.82549 -#> 5 15.99223 -#> 6 39.82549
    ## change between arbitrary values of mtcars$hp -dydx(head(mtcars), x, "hp", change = c(75,150))
    #> dydx_hp -#> 1 51.34102 -#> 2 51.34102 -#> 3 82.06566 -#> 4 51.34102 -#> 5 20.61638 -#> 6 51.34102
    +dydx(head(mtcars), x, "hp", change = "minmax")
    #> Error in prediction(model = model, data = data.table::rbindlist(list(d0, d1)), type = type, calculate_se = FALSE, ...): could not find function "prediction"
    ## change from 1st quartile to 3rd quartile +dydx(head(mtcars), x, "hp", change = "iqr")
    #> Error in prediction(model = model, data = data.table::rbindlist(list(d0, d1)), type = type, calculate_se = FALSE, ...): could not find function "prediction"
    ## change from mean(mtcars$hp) +/- sd(mtcars$hp) +dydx(head(mtcars), x, "hp", change = "sd")
    #> Error in prediction(model = model, data = data.table::rbindlist(list(d0, d1)), type = type, calculate_se = FALSE, ...): could not find function "prediction"
    ## change between arbitrary values of mtcars$hp +dydx(head(mtcars), x, "hp", change = c(75,150))
    #> Error in prediction(model = model, data = data.table::rbindlist(list(d0, d1)), type = type, calculate_se = FALSE, ...): could not find function "prediction"
    # factor variables mtcars[["cyl"]] <- factor(mtcars$cyl) x <- lm(mpg ~ cyl, data = head(mtcars)) -dydx(head(mtcars), x, "cyl")
    #> dydx_cyl6 dydx_cyl8 -#> 1 -2.425 -4.1 -#> 2 -2.425 -4.1 -#> 3 -2.425 -4.1 -#> 4 -2.425 -4.1 -#> 5 -2.425 -4.1 -#> 6 -2.425 -4.1
    +dydx(head(mtcars), x, "cyl")
    #> Error in prediction(model = model, data = d0, type = type, calculate_se = FALSE, ...): could not find function "prediction"
    + + diff --git a/docs/reference/index.html b/docs/reference/index.html index 5d62a64..98547ea 100644 --- a/docs/reference/index.html +++ b/docs/reference/index.html @@ -25,7 +25,12 @@ + + + + + @@ -48,8 +53,12 @@ - margins + + margins + 0.3.23 + + @@ -96,76 +106,71 @@ -
    -
    +
    +
    -
    -
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    -

    All functions

    -

    -
    -

    alexseev

    -

    Example Data

    -

    cplot()

    -

    Conditional predicted value and average marginal effect plots for models

    -

    dydx()

    -

    Marginal Effect of a Given Variable

    -

    marginal_effects()

    -

    Differentiate a Model Object with Respect to All Variables

    -

    margins()

    -

    Marginal Effects Estimation

    -

    image(<lm>) image(<glm>) image(<loess>) persp(<lm>) persp(<glm>) persp(<loess>)

    -

    Perspective and heatmap/contour plots for models

    -

    plot(<margins>)

    -

    Plot Marginal Effects Estimates

    -
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    +

    All functions

    +

    +
    +

    alexseev

    +

    Example Data

    +

    cplot()

    +

    Conditional predicted value and average marginal effect plots for models

    +

    dydx()

    +

    Marginal Effect of a Given Variable

    +

    marginal_effects()

    +

    Differentiate a Model Object with Respect to All (or Specified) Variables

    +

    margins()

    +

    Marginal Effects Estimation

    +

    image(<lm>) image(<glm>) image(<loess>) persp(<lm>) persp(<glm>) persp(<loess>)

    +

    Perspective and heatmap/contour plots for models

    +

    plot(<margins>)

    +

    Plot Marginal Effects Estimates

    + + diff --git a/docs/reference/marginal_effects.html b/docs/reference/marginal_effects.html index 728c91e..11b60f0 100644 --- a/docs/reference/marginal_effects.html +++ b/docs/reference/marginal_effects.html @@ -6,7 +6,7 @@ -Differentiate a Model Object with Respect to All Variables — marginal_effects • margins +Differentiate a Model Object with Respect to All (or Specified) Variables — marginal_effects • margins @@ -25,10 +25,15 @@ - + + + + + + @@ -51,8 +56,12 @@ - margins + + margins + 0.3.23 +
    +
    @@ -99,16 +109,19 @@ -
    +
    +

    Extract marginal effects from a model object, conditional on data, using dydx.

    +
    marginal_effects(model, data, variables = NULL, ...)
     
    @@ -116,41 +129,46 @@ 

    Differentiate a Model Object with Respect to All Variables

    marginal_effects(model, data, variables = NULL, ...) # S3 method for default -marginal_effects(model, data = find_data(model, +marginal_effects(model, data = find_data(model, parent.frame()), variables = NULL, type = c("response", "link"), eps = 1e-07, as.data.frame = TRUE, varslist = NULL, ...) # S3 method for glm -marginal_effects(model, data = find_data(model, parent.frame()), +marginal_effects(model, data = find_data(model, parent.frame()), variables = NULL, type = c("response", "link"), eps = 1e-07, as.data.frame = TRUE, varslist = NULL, ...) # S3 method for lm -marginal_effects(model, data = find_data(model, parent.frame()), +marginal_effects(model, data = find_data(model, parent.frame()), variables = NULL, type = c("response", "link"), eps = 1e-07, as.data.frame = TRUE, varslist = NULL, ...) # S3 method for loess -marginal_effects(model, data = find_data(model, +marginal_effects(model, data = find_data(model, parent.frame()), variables = NULL, type = c("response", "link"), eps = 1e-07, as.data.frame = TRUE, varslist = NULL, ...) # S3 method for merMod -marginal_effects(model, data = find_data(model), +marginal_effects(model, data = find_data(model), variables = NULL, type = c("response", "link"), eps = 1e-07, - as.data.frame = TRUE, ...) + as.data.frame = TRUE, varslist = NULL, ...) + +# S3 method for lmerMod +marginal_effects(model, data = find_data(model), + variables = NULL, type = c("response", "link"), eps = 1e-07, + as.data.frame = TRUE, varslist = NULL, ...) # S3 method for nnet -marginal_effects(model, data = find_data(model, +marginal_effects(model, data = find_data(model, parent.frame()), variables = NULL, eps = 1e-07, varslist = NULL, as.data.frame = TRUE, ...) # S3 method for polr -marginal_effects(model, data = find_data(model, +marginal_effects(model, data = find_data(model, parent.frame()), variables = NULL, eps = 1e-07, varslist = NULL, as.data.frame = TRUE, ...)
    -

    Arguments

    +

    Arguments

    @@ -193,98 +211,35 @@

    Value

    Details

    -

    This function extracts unit-specific marginal effects from an estimated model with respect to all variables specified in data and returns a data.frame. (Note that this is not each coefficient.) See dydx for computational details, or to extract the marginal effect for only one variable. Note that for factor and logical class variables, discrete changes in the outcome are reported rather than instantaneous marginal effects.

    +

    Users likely want to use the fully featured margins function rather than marginal_effects, which merely performs estimation of the marginal effects but simply returns a data frame. margins, by contrast, does some convenient packaging around these results and supports additional functionality, like variance estimation and counterfactual estimation procedures. The methods for this function provide lower-level functionality that extracts unit-specific marginal effects from an estimated model with respect to all variables specified in data (or the subset specified in variables) and returns a data frame. See dydx for computational details. Note that for factor and logical class variables, discrete changes in the outcome are reported rather than instantaneous marginal effects.

    Methods are currently implemented for the following object classes:

    -

    A methods is also provided for the object classes “margins” to return a simplified data frame from complete “margins” objects.

    +

    A method is also provided for the object classes “margins” to return a simplified data frame from complete “margins” objects.

    See also

    -

    dydx, margins

    +

    Examples

    require("datasets") x <- lm(mpg ~ cyl * hp + wt, data = mtcars) -marginal_effects(x)
    #> dydx_cyl dydx_hp dydx_wt -#> 1 -0.6572244 -0.04987248 -3.119815 -#> 2 -0.6572244 -0.04987248 -3.119815 -#> 3 -0.9794364 -0.08777977 -3.119815 -#> 4 -0.6572244 -0.04987248 -3.119815 -#> 5 0.5747624 -0.01196519 -3.119815 -#> 6 -0.7519926 -0.04987248 -3.119815 -#> 7 1.9015174 -0.01196519 -3.119815 -#> 8 -1.5669993 -0.08777977 -3.119815 -#> 9 -0.9415291 -0.08777977 -3.119815 -#> 10 -0.4108271 -0.04987248 -3.119815 -#> 11 -0.4108271 -0.04987248 -3.119815 -#> 12 0.6695306 -0.01196519 -3.119815 -#> 13 0.6695306 -0.01196519 -3.119815 -#> 14 0.6695306 -0.01196519 -3.119815 -#> 15 1.1433716 -0.01196519 -3.119815 -#> 16 1.3329081 -0.01196519 -3.119815 -#> 17 1.6172127 -0.01196519 -3.119815 -#> 18 -1.4911847 -0.08777977 -3.119815 -#> 19 -1.7565357 -0.08777977 -3.119815 -#> 20 -1.5101384 -0.08777977 -3.119815 -#> 21 -0.9036218 -0.08777977 -3.119815 -#> 22 0.1009213 -0.01196519 -3.119815 -#> 23 0.1009213 -0.01196519 -3.119815 -#> 24 1.9015174 -0.01196519 -3.119815 -#> 25 0.5747624 -0.01196519 -3.119815 -#> 26 -1.4911847 -0.08777977 -3.119815 -#> 27 -1.0173436 -0.08777977 -3.119815 -#> 28 -0.6003635 -0.08777977 -3.119815 -#> 29 2.2616366 -0.01196519 -3.119815 -#> 30 0.5747624 -0.04987248 -3.119815 -#> 31 3.6073452 -0.01196519 -3.119815 -#> 32 -0.6761781 -0.08777977 -3.119815
    +marginal_effects(x)
    #> Error in find_data(model, parent.frame()): could not find function "find_data"
    # factor variables report discrete differences x <- lm(mpg ~ factor(cyl) * factor(am), data = mtcars) -marginal_effects(x)
    #> dydx_cyl6 dydx_cyl8 dydx_am1 -#> 1 -7.508333 -12.675 1.441667 -#> 2 -7.508333 -12.675 1.441667 -#> 3 -7.508333 -12.675 5.175000 -#> 4 -3.775000 -7.850 1.441667 -#> 5 -3.775000 -7.850 0.350000 -#> 6 -3.775000 -7.850 1.441667 -#> 7 -3.775000 -7.850 0.350000 -#> 8 -3.775000 -7.850 5.175000 -#> 9 -3.775000 -7.850 5.175000 -#> 10 -3.775000 -7.850 1.441667 -#> 11 -3.775000 -7.850 1.441667 -#> 12 -3.775000 -7.850 0.350000 -#> 13 -3.775000 -7.850 0.350000 -#> 14 -3.775000 -7.850 0.350000 -#> 15 -3.775000 -7.850 0.350000 -#> 16 -3.775000 -7.850 0.350000 -#> 17 -3.775000 -7.850 0.350000 -#> 18 -7.508333 -12.675 5.175000 -#> 19 -7.508333 -12.675 5.175000 -#> 20 -7.508333 -12.675 5.175000 -#> 21 -3.775000 -7.850 5.175000 -#> 22 -3.775000 -7.850 0.350000 -#> 23 -3.775000 -7.850 0.350000 -#> 24 -3.775000 -7.850 0.350000 -#> 25 -3.775000 -7.850 0.350000 -#> 26 -7.508333 -12.675 5.175000 -#> 27 -7.508333 -12.675 5.175000 -#> 28 -7.508333 -12.675 5.175000 -#> 29 -7.508333 -12.675 0.350000 -#> 30 -7.508333 -12.675 1.441667 -#> 31 -7.508333 -12.675 0.350000 -#> 32 -7.508333 -12.675 5.175000
    +marginal_effects(x)
    #> Error in find_data(model, parent.frame()): could not find function "find_data"
    # get just marginal effects from "margins" object require('datasets') -m <- margins(lm(mpg ~ hp, data = mtcars[1:10,])) -marginal_effects(m)
    #> Average marginal effects
    #> lm(formula = mpg ~ hp, data = mtcars[1:10, ])
    #> hp -#> -0.0505
    marginal_effects(m)
    #> Average marginal effects
    #> lm(formula = mpg ~ hp, data = mtcars[1:10, ])
    #> hp -#> -0.0505
    +m <- margins(lm(mpg ~ hp, data = mtcars[1:10,]))
    #> Error in build_datalist(data, at = at): could not find function "build_datalist"
    marginal_effects(m)
    #> Error in marginal_effects(m): object 'm' not found
    marginal_effects(m)
    #> Error in marginal_effects(m): object 'm' not found
    # multi-category outcome if (requireNamespace("nnet")) { data("iris3", package = "datasets") @@ -294,157 +249,7 @@

    Examp decay = 5e-4, maxit = 200, trace = FALSE) marginal_effects(m) # default marginal_effects(m, category = "v") # explicit category -}

    #> Loading required namespace: nnet
    #> dydx_Sepal.L. dydx_Sepal.W. dydx_Petal.L. dydx_Petal.W. -#> 1 -7.051271e-07 -2.052312e-06 3.514936e-06 1.675324e-06 -#> 2 -2.151130e-06 -6.261015e-06 1.072339e-05 5.109200e-06 -#> 3 -1.177273e-06 -3.426522e-06 5.868559e-06 2.796840e-06 -#> 4 -3.090140e-06 -8.994083e-06 1.540458e-05 7.338492e-06 -#> 5 -6.224677e-07 -1.811726e-06 3.102878e-06 1.479004e-06 -#> 6 -9.843186e-07 -2.864892e-06 4.906381e-06 2.339968e-06 -#> 7 -1.397017e-06 -4.066084e-06 6.963751e-06 3.319857e-06 -#> 8 -1.271772e-06 -3.701575e-06 6.339730e-06 3.020847e-06 -#> 9 -3.808188e-06 -1.108402e-05 1.898409e-05 9.043857e-06 -#> 10 -2.106436e-06 -6.130955e-06 1.050094e-05 5.001401e-06 -#> 11 -5.492563e-07 -1.598641e-06 2.737944e-06 1.305000e-06 -#> 12 -2.052301e-06 -5.973374e-06 1.023083e-05 4.873978e-06 -#> 13 -1.954305e-06 -5.688160e-06 9.742478e-06 4.640471e-06 -#> 14 -9.948020e-07 -2.895437e-06 4.959052e-06 2.362935e-06 -#> 15 -9.403195e-08 -2.736807e-07 4.686752e-07 2.236826e-07 -#> 16 -1.668465e-07 -4.856028e-07 8.315335e-07 3.971989e-07 -#> 17 -2.702323e-07 -7.865056e-07 1.346799e-06 6.432828e-07 -#> 18 -8.240693e-07 -2.398486e-06 4.107675e-06 1.958711e-06 -#> 19 -8.362528e-07 -2.433956e-06 4.168526e-06 1.987103e-06 -#> 20 -6.447290e-07 -1.876509e-06 3.213725e-06 1.532467e-06 -#> 21 -1.912312e-06 -5.565928e-06 9.533023e-06 4.541303e-06 -#> 22 -9.122039e-07 -2.654990e-06 4.546800e-06 2.169108e-06 -#> 23 -2.235473e-07 -6.506385e-07 1.114237e-06 5.316339e-07 -#> 24 -4.944118e-06 -1.439005e-05 2.464453e-05 1.175222e-05 -#> 25 -6.227421e-06 -1.812545e-05 3.104518e-05 1.478457e-05 -#> 26 -4.123226e-06 -1.200098e-05 2.055484e-05 9.790796e-06 -#> 27 -2.491063e-06 -7.250352e-06 1.241719e-05 5.920211e-06 -#> 28 -9.164195e-07 -2.667296e-06 4.568269e-06 2.176984e-06 -#> 29 -7.992454e-07 -2.326250e-06 3.984119e-06 1.898853e-06 -#> 30 -3.342657e-06 -9.729062e-06 1.666351e-05 7.937629e-06 -#> 31 -3.850823e-06 -1.120812e-05 1.919684e-05 9.144097e-06 -#> 32 -1.337868e-06 -3.893906e-06 6.668626e-06 3.180636e-06 -#> 33 -2.538395e-07 -7.388152e-07 1.265359e-06 6.030371e-07 -#> 34 -1.477495e-07 -4.300291e-07 7.364530e-07 3.512868e-07 -#> 35 -2.490459e-06 -7.248663e-06 1.241507e-05 5.914606e-06 -#> 36 -6.942313e-07 -2.020594e-06 3.460564e-06 1.649712e-06 -#> 37 -3.939564e-07 -1.146627e-06 1.963742e-06 9.362959e-07 -#> 38 -5.692382e-07 -1.656806e-06 2.837639e-06 1.352058e-06 -#> 39 -2.153387e-06 -6.267576e-06 1.073456e-05 5.114918e-06 -#> 40 -1.189086e-06 -3.460914e-06 5.927537e-06 2.824494e-06 -#> 41 -6.348122e-07 -1.847640e-06 3.164226e-06 1.509214e-06 -#> 42 -1.170198e-05 -3.405942e-05 5.833446e-05 2.779557e-05 -#> 43 -1.441398e-06 -4.195279e-06 7.185237e-06 3.424127e-06 -#> 44 -2.842473e-06 -8.273009e-06 1.416714e-05 6.763525e-06 -#> 45 -2.940437e-06 -8.558310e-06 1.465764e-05 6.986092e-06 -#> 46 -2.732932e-06 -7.954356e-06 1.362327e-05 6.493020e-06 -#> 47 -7.647180e-07 -2.225759e-06 3.812052e-06 1.816598e-06 -#> 48 -1.768016e-06 -5.145929e-06 8.813511e-06 4.199491e-06 -#> 49 -5.860945e-07 -1.705861e-06 2.921582e-06 1.392495e-06 -#> 50 -1.106556e-06 -3.220700e-06 5.516077e-06 2.628692e-06 -#> 51 -3.308270e-04 -9.328372e-04 1.268526e-03 2.578749e-03 -#> 52 -6.772191e-04 -1.909681e-03 2.598219e-03 5.271903e-03 -#> 53 -4.837141e-03 -1.364451e-02 1.861278e-02 3.739975e-02 -#> 54 -6.509447e-04 -1.835797e-03 2.500036e-03 5.055018e-03 -#> 55 -5.670180e-03 -1.599418e-02 2.181636e-02 4.384958e-02 -#> 56 -9.822168e-04 -2.770426e-03 3.777054e-03 7.605369e-03 -#> 57 -5.170755e-03 -1.458489e-02 1.988804e-02 4.001904e-02 -#> 58 -3.220893e-06 -7.969617e-06 -1.736563e-06 9.145689e-05 -#> 59 -3.655220e-04 -1.030815e-03 1.403428e-03 2.840400e-03 -#> 60 -3.834587e-04 -1.080993e-03 1.467167e-03 3.003948e-03 -#> 61 -4.036631e-05 -1.134874e-04 1.505509e-04 3.345659e-04 -#> 62 -6.045658e-04 -1.704574e-03 2.316521e-03 4.720239e-03 -#> 63 -5.451886e-05 -1.535600e-04 2.069272e-04 4.349442e-04 -#> 64 -3.581290e-03 -1.010205e-02 1.378073e-02 2.768809e-02 -#> 65 -2.159420e-05 -5.959600e-05 6.642147e-05 2.454700e-04 -#> 66 -1.815702e-04 -5.116778e-04 6.924382e-04 1.433088e-03 -#> 67 -5.205039e-03 -1.468195e-02 2.002441e-02 4.026319e-02 -#> 68 -2.883766e-05 -8.103599e-05 1.070559e-04 2.413568e-04 -#> 69 -1.368537e-01 -3.860565e-01 5.268793e-01 1.056814e+00 -#> 70 -5.121478e-05 -1.441034e-04 1.924830e-04 4.175505e-04 -#> 71 -5.785503e-01 -1.632036e+00 2.227167e+00 4.467869e+00 -#> 72 -8.154203e-05 -2.294451e-04 3.065870e-04 6.642272e-04 -#> 73 -4.004084e-01 -1.129536e+00 1.541658e+00 3.091197e+00 -#> 74 -5.829030e-04 -1.644143e-03 2.241718e-03 4.512465e-03 -#> 75 -1.372603e-04 -3.867941e-04 5.232665e-04 1.084256e-03 -#> 76 -2.688252e-04 -7.578365e-04 1.028589e-03 2.105792e-03 -#> 77 -3.391752e-03 -9.567441e-03 1.305180e-02 2.622076e-02 -#> 78 -4.677926e-01 -1.319612e+00 1.800950e+00 3.612012e+00 -#> 79 -4.252792e-03 -1.199579e-02 1.635925e-02 3.290525e-02 -#> 80 3.816546e-06 1.214760e-05 -3.218662e-05 5.292630e-05 -#> 81 -5.093067e-05 -1.432755e-04 1.910547e-04 4.169323e-04 -#> 82 -2.005929e-05 -5.608837e-05 7.092490e-05 1.845717e-04 -#> 83 -5.054036e-05 -1.420422e-04 1.878771e-04 4.218079e-04 -#> 84 -2.902382e-01 -8.187566e-01 1.117553e+00 2.240436e+00 -#> 85 -7.417869e-03 -2.092409e-02 2.854210e-02 5.735885e-02 -#> 86 -1.556468e-03 -4.389470e-03 5.976695e-03 1.209248e-02 -#> 87 -1.925287e-03 -5.430371e-03 7.402756e-03 1.491167e-02 -#> 88 -1.803992e-03 -5.088553e-03 6.940172e-03 1.395433e-02 -#> 89 -1.086502e-04 -3.059936e-04 4.119407e-04 8.688880e-04 -#> 90 -3.336326e-04 -9.406950e-04 1.278610e-03 2.603794e-03 -#> 91 -5.896383e-04 -1.663092e-03 2.267004e-03 4.567511e-03 -#> 92 -1.284914e-03 -3.624165e-03 4.940486e-03 9.951931e-03 -#> 93 -8.921303e-05 -2.512698e-04 3.384666e-04 7.124064e-04 -#> 94 -6.027464e-06 -1.600382e-05 1.055078e-05 1.061463e-04 -#> 95 -3.757469e-04 -1.059556e-03 1.441494e-03 2.925476e-03 -#> 96 -7.176199e-05 -2.020605e-04 2.715224e-04 5.765191e-04 -#> 97 -1.857794e-04 -5.236525e-04 7.099202e-04 1.459576e-03 -#> 98 -1.660438e-04 -4.680036e-04 6.342455e-04 1.305740e-03 -#> 99 2.920859e-05 8.613326e-05 -1.597838e-04 -2.591674e-06 -#> 100 -1.678648e-04 -4.730807e-04 6.404995e-04 1.323365e-03 -#> 101 -2.654112e-06 -7.487187e-06 1.021943e-05 2.048771e-05 -#> 102 -1.310598e-03 -3.697186e-03 5.046558e-03 1.011684e-02 -#> 103 -3.838546e-05 -1.082849e-04 1.478051e-04 2.962896e-04 -#> 104 -1.111922e-03 -3.136731e-03 4.281638e-03 8.582727e-03 -#> 105 -1.503573e-05 -4.241555e-05 5.789518e-05 1.160592e-04 -#> 106 -5.907586e-06 -1.666524e-05 2.274763e-05 4.559757e-05 -#> 107 -2.283101e-01 -6.440560e-01 8.790641e-01 1.762618e+00 -#> 108 -8.471160e-05 -2.389710e-04 3.261938e-04 6.538452e-04 -#> 109 -1.086850e-04 -3.065999e-04 4.185060e-04 8.388925e-04 -#> 110 -6.478197e-06 -1.827482e-05 2.494354e-05 5.000787e-05 -#> 111 -2.164049e-02 -6.104735e-02 8.332414e-02 1.670762e-01 -#> 112 -9.602287e-04 -2.708799e-03 3.697457e-03 7.412093e-03 -#> 113 -1.743856e-04 -4.919392e-04 6.714732e-04 1.346120e-03 -#> 114 -2.281324e-04 -6.435581e-04 8.784256e-04 1.761014e-03 -#> 115 -1.366213e-05 -3.854036e-05 5.260243e-05 1.054739e-04 -#> 116 -7.842880e-05 -2.212452e-04 3.019751e-04 6.054726e-04 -#> 117 -6.237476e-03 -1.759592e-02 2.401846e-02 4.814750e-02 -#> 118 -1.492471e-05 -4.210243e-05 5.746856e-05 1.151982e-04 -#> 119 -6.607927e-07 -1.864090e-06 2.544454e-06 5.100176e-06 -#> 120 -1.758884e-01 -4.961804e-01 6.772784e-01 1.357688e+00 -#> 121 -2.540746e-05 -7.167388e-05 9.782958e-05 1.961284e-04 -#> 122 -1.523471e-03 -4.297688e-03 5.866069e-03 1.176092e-02 -#> 123 -5.597061e-06 -1.578926e-05 2.155201e-05 4.320040e-05 -#> 124 -1.088174e-01 -3.069719e-01 4.189939e-01 8.400831e-01 -#> 125 -1.686582e-04 -4.757827e-04 6.494236e-04 1.301891e-03 -#> 126 -1.633639e-03 -4.608498e-03 6.290638e-03 1.260966e-02 -#> 127 -3.414447e-01 -9.631992e-01 1.314599e+00 2.636225e+00 -#> 128 -3.874637e-01 -1.093016e+00 1.491769e+00 2.991506e+00 -#> 129 -3.426615e-05 -9.666432e-05 1.319427e-04 2.644967e-04 -#> 130 -7.313048e-02 -2.063014e-01 2.816048e-01 5.644852e-01 -#> 131 -6.781546e-05 -1.913070e-04 2.611313e-04 5.234379e-04 -#> 132 -4.790724e-04 -1.351461e-03 1.844734e-03 3.697861e-03 -#> 133 -1.648056e-05 -4.649136e-05 6.345819e-05 1.272133e-04 -#> 134 -3.571999e-01 -1.007648e+00 1.375315e+00 2.757588e+00 -#> 135 -8.888042e-02 -2.507323e-01 3.422558e-01 6.860416e-01 -#> 136 -8.383570e-06 -2.364991e-05 3.228094e-05 6.471176e-05 -#> 137 -1.720157e-05 -4.852511e-05 6.623179e-05 1.327913e-04 -#> 138 -9.259390e-03 -2.612075e-02 3.565482e-02 7.147442e-02 -#> 139 -5.446042e-01 -1.536289e+00 2.096638e+00 4.205074e+00 -#> 140 -5.693511e-04 -1.606130e-03 2.192276e-03 4.395136e-03 -#> 141 -1.178656e-05 -3.324955e-05 4.538246e-05 9.098726e-05 -#> 142 -3.043450e-04 -8.585449e-04 1.171781e-03 2.349814e-03 -#> 143 -1.310598e-03 -3.697186e-03 5.046558e-03 1.011684e-02 -#> 144 -1.177644e-05 -3.322111e-05 4.534485e-05 9.090278e-05 -#> 145 -7.455583e-06 -2.103195e-05 2.870623e-05 5.755569e-05 -#> 146 -9.105744e-05 -2.568700e-04 3.505966e-04 7.029808e-04 -#> 147 -2.428161e-03 -6.849821e-03 9.349784e-03 1.874402e-02 -#> 148 -2.783418e-03 -7.851988e-03 1.071757e-02 2.148720e-02 -#> 149 -7.646882e-05 -2.157163e-04 2.944303e-04 5.903333e-04 -#> 150 -5.193763e-02 -1.465156e-01 1.999884e-01 4.009439e-01
    +}
    #> Loading required namespace: nnet
    #> Error in find_data(model, parent.frame()): could not find function "find_data"
    + + diff --git a/docs/reference/margins.html b/docs/reference/margins.html index 47f0cf8..5d4a14c 100644 --- a/docs/reference/margins.html +++ b/docs/reference/margins.html @@ -25,11 +25,16 @@ + + + - + + + @@ -52,8 +57,12 @@ - margins + + margins + 0.3.23 + + @@ -100,65 +110,79 @@ -
    +
    +
    -

    This package is an R port of Stata's margins command, implemented as an S3 generic margins() for model objects, like those of class “lm” and “glm”. margins() is an S3 generic function for building a “margins” object from a model object. Methods are currently implemented for “lm” (and, implicitly, “glm”) class objects and support is expanding. See Details, below.

    -

    The package also provides a low-level function, marginal_effects, to estimate those quantities and return a data frame of unit-specific effects and another, dydx, to provide variable-specific derivatives from models. Some of the underlying architecture for the package is provided by the low-level function prediction, which provides a consistent data frame interface to predict for a large number of model types.

    +

    This package is an R port of Stata's margins command, implemented as an S3 generic margins() for model objects, like those of class “lm” and “glm”. margins() is an S3 generic function for building a “margins” object from a model object. Methods are currently implemented for several model classes (see Details, below).

    +

    The package also provides a low-level function, marginal_effects, to estimate those quantities and return a data frame of unit-specific effects and another even lower-level function, dydx, to provide variable-specific derivatives from models. Some of the underlying architecture for the package is provided by the low-level function prediction, which provides a consistent data frame interface to predict for a large number of model types. If a prediction method exists for a model class, margin should work for the model class but only those classes listed here have been tested and specifically supported.

    +
    margins(model, ...)
     
     # S3 method for betareg
    -margins(model, data = find_data(model, parent.frame()),
    -  variables = NULL, at = NULL, type = c("response", "link", "terms"),
    +margins(model, data = find_data(model, parent.frame()),
    +  variables = NULL, at = NULL, type = c("response", "link"),
       vcov = stats::vcov(model, phi = FALSE), vce = c("delta", "simulation",
       "bootstrap", "none"), iterations = 50L, unit_ses = FALSE, eps = 1e-07,
       ...)
     
     # S3 method for default
    -margins(model, data = find_data(model, parent.frame()),
    -  variables = NULL, at = NULL, type = c("response", "link", "terms"),
    -  vcov = stats::vcov(model), vce = c("none", "delta", "simulation",
    -  "bootstrap"), iterations = 50L, unit_ses = FALSE, eps = 1e-07, ...)
    +margins(model, data = find_data(model, parent.frame()),
    +  variables = NULL, at = NULL, type = c("response", "link"),
    +  vcov = stats::vcov(model), vce = c("delta", "simulation", "bootstrap",
    +  "none"), iterations = 50L, unit_ses = FALSE, eps = 1e-07, ...)
     
     # S3 method for glm
    -margins(model, data = find_data(model, parent.frame()),
    -  variables = NULL, at = NULL, type = c("response", "link", "terms"),
    +margins(model, data = find_data(model, parent.frame()),
    +  variables = NULL, at = NULL, type = c("response", "link"),
       vcov = stats::vcov(model), vce = c("delta", "simulation", "bootstrap",
       "none"), iterations = 50L, unit_ses = FALSE, eps = 1e-07, ...)
     
     # S3 method for lm
    -margins(model, data = find_data(model, parent.frame()),
    -  variables = NULL, at = NULL, type = c("response", "link", "terms"),
    +margins(model, data = find_data(model, parent.frame()),
    +  variables = NULL, at = NULL, type = c("response", "link"),
       vcov = stats::vcov(model), vce = c("delta", "simulation", "bootstrap",
       "none"), iterations = 50L, unit_ses = FALSE, eps = 1e-07, ...)
     
     # S3 method for loess
     margins(model, data, variables = NULL, at = NULL,
    -  eps = 1e-07, ...)
    +  vce = "none", eps = 1e-07, ...)
    +
    +# S3 method for merMod
    +margins(model, data = find_data(model), variables = NULL,
    +  at = NULL, type = c("response", "link"), vcov = stats::vcov(model),
    +  vce = c("delta", "simulation", "bootstrap", "none"), iterations = 50L,
    +  unit_ses = FALSE, eps = 1e-07, ...)
    +
    +# S3 method for lmerMod
    +margins(model, data = find_data(model), variables = NULL,
    +  at = NULL, type = c("response", "link"), vcov = stats::vcov(model),
    +  vce = c("delta", "simulation", "bootstrap", "none"), iterations = 50L,
    +  unit_ses = FALSE, eps = 1e-07, ...)
     
     # S3 method for nnet
    -margins(model, data = find_data(model, parent.frame()),
    +margins(model, data = find_data(model, parent.frame()),
       variables = NULL, at = NULL, vce = "none", eps = 1e-07, ...)
     
     # S3 method for polr
    -margins(model, data = find_data(model, parent.frame()),
    +margins(model, data = find_data(model, parent.frame()),
       variables = NULL, at = NULL, vce = "none", eps = 1e-07, ...)
     
     # S3 method for svyglm
    -margins(model, data = find_data(model, parent.frame()),
    -  design, variables = NULL, at = NULL, type = c("response", "link",
    -  "terms"), vcov = stats::vcov(model), vce = c("delta", "simulation",
    -  "bootstrap", "none"), iterations = 50L, unit_ses = FALSE, eps = 1e-07,
    -  ...)
    +margins(model, data = find_data(model, parent.frame()), + design, variables = NULL, at = NULL, type = c("response", "link"), + vcov = stats::vcov(model), vce = c("delta", "simulation", "bootstrap", + "none"), iterations = 50L, unit_ses = FALSE, eps = 1e-07, ...) -

    Arguments

    +

    Arguments

    @@ -213,23 +237,25 @@

    Ar

    Value

    -

    A data frame of class “margins” containing the contents of data, fitted values for model, the standard errors of the fitted values, and any estimated marginal effects. If at = NULL (the default), then the data frame will have a number of rows equal to nrow(data). Otherwise, the number of rows will be a multiple thereof based upon the intersection of values specified in at. Columns containing marginal effects are distinguished by their name (prefixed by dydx_). These columns can be extracted from a “margins” object using, for example, marginal_effects(margins(model)). Columns prefixed by Var_ specify the variances of the average marginal effects, whereas (optional) columns prefixed by SE_ contain observation-specific standard errors. A special list column, .at, will contain information on the combination of values from at reflected in each row observation. The summary.margins() method provides for pretty printing of the results. A variance-covariance matrix for the average marginal effects is returned as an attribute (though behavior when at is non-NULL is unspecified).

    +

    A data frame of class “margins” containing the contents of data, predicted values from model for data, the standard errors of the predictions, and any estimated marginal effects. If at = NULL (the default), then the data frame will have a number of rows equal to nrow(data). Otherwise, the number of rows will be a multiple thereof based upon the number of combinations of values specified in at. Columns containing marginal effects are distinguished by their name (prefixed by dydx_). These columns can be extracted from a “margins” object using, for example, marginal_effects(margins(model)). Columns prefixed by Var_ specify the variances of the average marginal effects, whereas (optional) columns prefixed by SE_ contain observation-specific standard errors. A special column, _at_number, specifies which at combination a given row corresponds to; the data frame carries an attribute “at” that specifies which combination of values this index represents. The summary.margins() method provides for pretty printing of the results, particularly in cases where at is specified. A variance-covariance matrix for the average marginal effects is returned as an attribute (though behavior when at is non-NULL is unspecified).

    Details

    -

    Methods for this generic return a “margins” object, which is a data frame consisting of the original data, predicted values and standard errors thereof, estimated marginal effects from the model model, with attributes describing various features of the marginal effects estimates.

    -

    Some modelling functions set model = FALSE by default. For margins to work best, this should be set to TRUE. Otherwise the data argument to margins is probably required.

    -

    See dydx for details on estimation of marginal effects.

    +

    Methods for this generic return a “margins” object, which is a data frame consisting of the original data, predicted values and standard errors thereof, estimated marginal effects from the model model (for all variables used in the model, or the subset specified by variables), along with attributes describing various features of the marginal effects estimates. The default print method is concise; a more useful summary method provides additional details.

    Methods are currently implemented for the following object classes:

    -

    The margins method for objects of class “lm” or “glm” simply constructs a list of data frames (using build_datalist), calculates marginal effects for each data frame (via marginal_effects and, in turn, prediction), and row-binds the results together. Alternatively, you can use marginal_effects to retrieve a data frame of marginal effects without constructing a “margins” object. That can be efficient for plotting, etc., given the time-consuming nature of variance estimation.

    -

    The choice of vce may be important. The default variance-covariance estimation procedure (vce = "delta") uses the delta method to estimate marginal effect variances. This is the fastest method. When vce = "simulation", coefficient estimates are repeatedly drawn from the asymptotic (multivariate normal) distribution of the model coefficients and each draw is used to estimate marginal effects, with the variance based upon the dispersion of those simulated effects. The number of interations used is given by iterations. For vce = "bootstrap", the bootstrap is used to repeatedly subsample data and the variance of marginal effects is estimated from the variance of the bootstrap distribution. This method is markedly slower than the other two procedures. Again, iterations regulates the number of bootstrap subsamples to draw.

    +

    The margins methods simply construct a list of data frames based upon the values of at (using build_datalist), calculate marginal effects for each data frame (via marginal_effects and, in turn, dydx and prediction), stacks the results together, and provides variance estimates. Alternatively, you can use marginal_effects directly to only retrieve a data frame of marginal effects without constructing a “margins” object or variance estimates. That can be efficient for plotting, etc., given the time-consuming nature of variance estimation.

    +

    See dydx for details on estimation of marginal effects.

    +

    The choice of vce may be important. The default variance-covariance estimation procedure (vce = "delta") uses the delta method to estimate marginal effect variances. This is the fastest method. When vce = "simulation", coefficient estimates are repeatedly drawn from the asymptotic (multivariate normal) distribution of the model coefficients and each draw is used to estimate marginal effects, with the variance based upon the dispersion of those simulated effects. The number of iterations used is given by iterations. For vce = "bootstrap", the bootstrap is used to repeatedly subsample data and the variance of marginal effects is estimated from the variance of the bootstrap distribution. This method is markedly slower than the other two procedures. Again, iterations regulates the number of bootstrap subsamples to draw. Some model classes (notably “loess”) fix vce ="none".

    References

    @@ -238,54 +264,32 @@

    R

    See also

    -

    marginal_effects, dydx, prediction

    +

    Examples

    # basic example using linear model require("datasets") x <- lm(mpg ~ cyl * hp + wt, data = head(mtcars)) -margins(x)
    #> Average marginal effects
    #> lm(formula = mpg ~ cyl * hp + wt, data = head(mtcars))
    #> cyl hp wt -#> -11.81 0.6845 0.6995
    +margins(x)
    #> Error in build_datalist(data, at = at): could not find function "build_datalist"
    # obtain unit-specific standard errors
    # NOT RUN { margins(x, unit_ses = TRUE) # }
    # use of 'variables' argument to estimate only some MEs -summary(margins(x, variables = "hp"))
    #> factor AME SE z p lower upper -#> hp 0.6845 0.0489 13.9991 0.0000 0.5887 0.7804
    +summary(margins(x, variables = "hp"))
    #> Error in build_datalist(data, at = at): could not find function "build_datalist"
    # use of 'at' argument ## modifying original data values -margins(x, at = list(hp = 150))
    #> Average marginal effects at specified values
    #> lm(formula = mpg ~ cyl * hp + wt, data = head(mtcars))
    #> at(hp) cyl hp wt -#> 150 -18.53 0.6845 0.6995
    ## AMEs at various data values -margins(x, at = list(hp = c(95, 150), cyl = c(4,6)))
    #> Average marginal effects at specified values
    #> lm(formula = mpg ~ cyl * hp + wt, data = head(mtcars))
    #> at(hp) at(cyl) cyl hp wt -#> 95 4 -7.266 1.0942 0.6995 -#> 150 4 -18.531 1.0942 0.6995 -#> 95 6 -7.266 0.6845 0.6995 -#> 150 6 -18.531 0.6845 0.6995
    +margins(x, at = list(hp = 150))
    #> Error in build_datalist(data, at = at): could not find function "build_datalist"
    ## AMEs at various data values +margins(x, at = list(hp = c(95, 150), cyl = c(4,6)))
    #> Error in build_datalist(data, at = at): could not find function "build_datalist"
    # use of 'data' argument to obtain AMEs for a subset of data -margins(x, data = mtcars[mtcars[["cyl"]] == 4,])
    #> Average marginal effects
    #> lm(formula = mpg ~ cyl * hp + wt, data = head(mtcars))
    #> cyl hp wt -#> -4.733 1.094 0.6995
    margins(x, data = mtcars[mtcars[["cyl"]] == 6,])
    #> Average marginal effects
    #> lm(formula = mpg ~ cyl * hp + wt, data = head(mtcars))
    #> cyl hp wt -#> -12.85 0.6845 0.6995
    +margins(x, data = mtcars[mtcars[["cyl"]] == 4,])
    #> Error in build_datalist(data, at = at): could not find function "build_datalist"
    margins(x, data = mtcars[mtcars[["cyl"]] == 6,])
    #> Error in build_datalist(data, at = at): could not find function "build_datalist"
    # return discrete differences for continuous terms ## passes 'change' through '...' to dydx() -margins(x, change = "sd")
    #> Average marginal effects
    #> lm(formula = mpg ~ cyl * hp + wt, data = head(mtcars))
    #> cyl hp wt -#> -29.87 39.83 0.6481
    +margins(x, change = "sd")
    #> Error in build_datalist(data, at = at): could not find function "build_datalist"
    # summary() method -summary(margins(x, at = list(hp = c(95, 150))))
    #> factor hp AME SE z p lower upper -#> cyl 95.0000 -7.2657 0.5156 -14.0925 0.0000 -8.2762 -6.2552 -#> cyl 150.0000 -18.5314 1.3065 -14.1836 0.0000 -21.0921 -15.9706 -#> hp 95.0000 0.6845 0.0489 13.9989 0.0000 0.5887 0.7804 -#> hp 150.0000 0.6845 0.0489 13.9986 0.0000 0.5887 0.7804 -#> wt 95.0000 0.6995 0.3304 2.1169 0.0343 0.0519 1.3472 -#> wt 150.0000 0.6995 0.3301 2.1193 0.0341 0.0526 1.3464
    ## control row order of summary() output -summary(margins(x, at = list(hp = c(95, 150))), by_factor = FALSE)
    #> factor hp AME SE z p lower upper -#> cyl 95.0000 -7.2657 0.5156 -14.0925 0.0000 -8.2762 -6.2552 -#> hp 95.0000 0.6845 0.0489 13.9989 0.0000 0.5887 0.7804 -#> wt 95.0000 0.6995 0.3304 2.1169 0.0343 0.0519 1.3472 -#> cyl 150.0000 -18.5314 1.3065 -14.1836 0.0000 -21.0921 -15.9706 -#> hp 150.0000 0.6845 0.0489 13.9986 0.0000 0.5887 0.7804 -#> wt 150.0000 0.6995 0.3301 2.1193 0.0341 0.0526 1.3464
    +summary(margins(x, at = list(hp = c(95, 150))))
    #> Error in build_datalist(data, at = at): could not find function "build_datalist"
    ## control row order of summary() output +summary(margins(x, at = list(hp = c(95, 150))), by_factor = FALSE)
    #> Error in build_datalist(data, at = at): could not find function "build_datalist"
    # alternative 'vce' estimation
    # NOT RUN { # bootstrap @@ -299,13 +303,10 @@

    Examp summary(margins(x2)) ## heteroskedasticity-consistent covariance matrix summary(margins(x2, vcov = vcovHC(x2))) -}
    #> Loading required package: sandwich
    #> factor AME SE z p lower upper -#> Sepal.Width 0.7224 0.3332 2.1681 0.0301 0.0694 1.3754
    +}
    #> Loading required package: sandwich
    #> Error in build_datalist(data, at = at): could not find function "build_datalist"
    # generalized linear model x <- glm(am ~ hp, data = head(mtcars), family = binomial) -margins(x, type = "response")
    #> Average marginal effects
    #> glm(formula = am ~ hp, family = binomial, data = head(mtcars))
    #> hp -#> -0.0168
    margins(x, type = "link")
    #> Average marginal effects
    #> glm(formula = am ~ hp, family = binomial, data = head(mtcars))
    #> hp -#> -0.08928
    +margins(x, type = "response")
    #> Error in build_datalist(data, at = at): could not find function "build_datalist"
    margins(x, type = "link")
    #> Error in build_datalist(data, at = at): could not find function "build_datalist"
    # multi-category outcome if (requireNamespace("nnet")) { data("iris3", package = "datasets") @@ -315,8 +316,7 @@

    Examp decay = 5e-4, maxit = 200, trace = FALSE) margins(m) # default margins(m, category = "v") # explicit category -}

    #> Average marginal effects
    #> nnet.formula(formula = species ~ ., data = ird, size = 2, rang = 0.1, decay = 5e-04, maxit = 200, trace = FALSE)
    #> Sepal.L. Sepal.W. Petal.L. Petal.W. -#> -0.02979 -0.08066 0.1131 0.2193
    +}
    #> Error in build_datalist(data, at = at): could not find function "build_datalist"

    + + diff --git a/docs/reference/persp.html b/docs/reference/persp.html index cbd447d..b89c01a 100644 --- a/docs/reference/persp.html +++ b/docs/reference/persp.html @@ -25,10 +25,15 @@ + + + + + @@ -51,8 +56,12 @@ - margins + + margins + 0.3.23 + + @@ -99,16 +109,19 @@ -
    +
    +

    Draw one or more perspectives plots reflecting predictions or marginal effects from a model, or the same using a flat heatmap or “filled contour” (image) representation. Currently methods exist for “lm”, “glm”, and “loess” models.

    +
    # S3 method for lm
     image(x, xvar = attributes(terms(x))[["term.labels"]][1],
    @@ -170,7 +183,7 @@ 

    Perspective and heatmap/contour plots for models

    "prediction") "Predicted value" else paste0("Marginal effect of ", dx), ticktype = c("detailed", "simple"), ...)
    -

    Arguments

    +

    Arguments

    @@ -293,7 +306,7 @@

    Ar

    See also

    -

    plot.margins, cplot

    +

    Examples

    @@ -348,6 +361,8 @@

    Contents

    + + diff --git a/docs/reference/plot.margins.html b/docs/reference/plot.margins.html index 79516a1..d80adb3 100644 --- a/docs/reference/plot.margins.html +++ b/docs/reference/plot.margins.html @@ -25,10 +25,15 @@ + + + + + @@ -51,8 +56,12 @@ - margins + + margins + 0.3.23 + + @@ -99,16 +109,19 @@ -
    +
    +

    An implementation of Stata's marginsplot as an S3 generic function

    +
    # S3 method for margins
     plot(x, pos = seq_along(marginal_effects(x, with_at =
    @@ -118,7 +131,7 @@ 

    Plot Marginal Effects Estimates

    points.col = "black", points.bg = "black", las = 1, cex = 1, lwd = 2, zeroline = TRUE, zero.col = "gray", ...)
    -

    Arguments

    +

    Arguments

    @@ -201,7 +214,7 @@

    Details

    See also

    -

    margins, persp.lm

    +

    Examples

    @@ -242,6 +255,8 @@

    Contents

    + + diff --git a/docs/reference/reexports.html b/docs/reference/reexports.html index ef09eea..e99d701 100644 --- a/docs/reference/reexports.html +++ b/docs/reference/reexports.html @@ -25,6 +25,9 @@ + + + + + @@ -55,8 +60,12 @@ - margins + + margins + 0.3.23 + + @@ -103,13 +113,15 @@ -
    +
    +

    These objects are imported from other packages. Follow the links below to see their documentation.

    @@ -117,6 +129,7 @@

    Objects exported from other packages

    prediction

    prediction, find_data

    +
    @@ -141,6 +154,8 @@

    Contents

    + +