项目作者: goshevs

项目描述 :
Pairwise comparisons of margins computed with Package 'effects' in R
高级语言: R
项目地址: git://github.com/goshevs/effcomp.git
创建时间: 2016-06-28T19:40:53Z
项目社区:https://github.com/goshevs/effcomp

开源协议:MIT License

下载


Introduction

Package ‘effects’ in R offers excellent functionality for computing the
type of marginal effects known as average partial effects. An
unfortunate shotfall of the package is that it does not offer
capabilities for computing and testing of pairwise differences in the
margins. The goal of this project is to fill this void.

Installation

Download and source effcomp.R into R.

Functionality

File effcomp.R contains one primary function and several utility
functions. Users would want to use only the primary function.

Primary function

The primary function is effcomp.

effcomp computes pairwise differences in margins and also reports
significance tests of differences in the margins, if requested. Its
syntax is:

  1. effcomp(effects_obj, lincon, all = FALSE, tests = FALSE, ...)

and its arguments are:

  • effects_obj: the object created by running either effect or
    Effect from Package ‘effects’

  • lincon: stands for “linear contrasts” and is a matrix of pairwise
    linear contrasts which is provided by the user. This argument can be
    omitted if all = TRUE

  • all: a logical flag which indicates whether all possible pairwise
    contrasts are to be computed. At its default value FALSE, the user
    has to provide a matrix of pairwise linear contrasts.

  • tests: a logical flag which indicates whether tests of
    significance of the differences in margins are to be computed. By
    default tests = FALSE

  • ...: if tests = TRUE, the user may wish pass specify a method
    for adjusting the p-values of the tests for multiple comparisons.
    This is done using the argument adjmethod which takes all methods
    described in p.adjust of base R as well as "sidak" and
    "scheffe". For no adjustment, use "none". The default method is
    "bonferroni".

Utility functions

Function testpwcomp computes the significance tests, p_adjust
computes the p-values adjusted for multiple comparisons.

Examples of usage

We start by removing all objects in memory and loading the effect
package.

  1. ## Clear memory and load package effects
  2. rm(list=ls())
  3. library(effects)

Next, we source effcomp.R

  1. # Source in effcomp.R
  2. source("effcomp.R")

Finally, we load an example dataset provided in the effects package
and make some minor changes to the variables

  1. ## Making minor changes to the data
  2. Prestige$educ <- round(Prestige$education)
  3. Prestige$educ <- ifelse(Prestige$educ <= 12, 12, Prestige$educ)
  4. Prestige$educ <- ifelse(Prestige$educ >= 14, 14, Prestige$educ)
  5. Prestige$educ <- as.factor(Prestige$educ)
  6. Prestige$income <- Prestige$income/1000

We are now ready to illustrate the usage of effcomp.

Contrasts of margins, one variable

We run our model and compute the margins of variable type.

  1. fit_lm <- lm(prestige ~ income + type + educ, data = Prestige)
  2. type_eff <- effect("type", fit_lm) # compute effects
  3. as.data.frame(type_eff) # print effects
  4. ## type fit se lower upper
  5. ## 1 bc 40.23627 1.450572 37.35531 43.11723
  6. ## 2 prof 58.14816 2.258361 53.66286 62.63346
  7. ## 3 wc 46.30919 1.811231 42.71193 49.90645

To obtain all possible contrasts of the margins of variable type, we
execute

  1. comps <- effcomp(type_eff, all = TRUE) # compute all contrasts
  2. summary(comps)
  3. ##
  4. ## Call:
  5. ## effcomp(effects_obj = type_eff, all = TRUE)
  6. ##
  7. ## Pairwise differences of margins.
  8. ##
  9. ## Output:
  10. ## Contrast SE
  11. ## prof vs bc 17.9119 3.1957
  12. ## wc vs bc 6.0729 2.0338
  13. ## wc vs prof -11.8390 3.3361

If we also want to test whether the margins are different from each
other, we run

  1. comps <- effcomp(type_eff, all = TRUE, tests = TRUE) # Bonferroni adjustment, default
  2. summary(comps)
  3. ##
  4. ## Call:
  5. ## effcomp(effects_obj = type_eff, all = TRUE, tests = TRUE)
  6. ##
  7. ## Pairwise differences of margins.
  8. ##
  9. ## Output:
  10. ## Contrast SE t value Pr(>|t|) AdjPr(>|t|)
  11. ## prof vs bc 17.9119 3.1957 5.6049 0.0000 0.0000
  12. ## wc vs bc 6.0729 2.0338 2.9860 0.0036 0.0109
  13. ## wc vs prof -11.8390 3.3361 -3.5487 0.0006 0.0018

In case we want to use a different adjustment, for example Scheffe’s, we
specify the command in the following way:

  1. ## Scheffe adjustment
  2. comps <- effcomp(type_eff, all = TRUE, tests = TRUE, adjmethod ="scheffe")
  3. summary(comps)
  4. ##
  5. ## Call:
  6. ## effcomp(effects_obj = type_eff, all = TRUE, tests = TRUE, adjmethod = "scheffe")
  7. ##
  8. ## Pairwise differences of margins.
  9. ##
  10. ## Output:
  11. ## Contrast SE t value Pr(>|t|) AdjPr(>|t|)
  12. ## prof vs bc 17.9119 3.1957 5.6049 0.0000 0.0000
  13. ## wc vs bc 6.0729 2.0338 2.9860 0.0036 0.0142
  14. ## wc vs prof -11.8390 3.3361 -3.5487 0.0006 0.0027

All standard methods for adjustment are available. Sidak’s method has
also be added.

effcomp also accepts a user provided matrix for constructing desired
contrasts.

  1. contr_mat <- matrix(c(-1, 1, 0,
  2. -1, 0, 1),
  3. nrow = 2, byrow = TRUE)
  4. comps <- effcomp(type_eff,contr_mat) # compute contrasts
  5. summary(comps)
  6. ##
  7. ## Call:
  8. ## effcomp(effects_obj = type_eff, lincon = contr_mat)
  9. ##
  10. ## Pairwise differences of margins.
  11. ##
  12. ## Output:
  13. ## Contrast SE
  14. ## prof vs bc 17.9119 3.1957
  15. ## wc vs bc 6.0729 2.0338

To test the differences in the margins (and apply adjustments for
multiple comparisons), we use the following syntax:

  • No adjustment for multiple comparisons
  1. ## No adjustment for multiple comparisons
  2. comps <- effcomp(type_eff, contr_mat, tests = TRUE, adjmethod = "none")
  3. summary(comps)
  4. ##
  5. ## Call:
  6. ## effcomp(effects_obj = type_eff, lincon = contr_mat, tests = TRUE,
  7. ## adjmethod = "none")
  8. ##
  9. ## Pairwise differences of margins.
  10. ##
  11. ## Output:
  12. ## Contrast SE t value Pr(>|t|)
  13. ## prof vs bc 17.9119 3.1957 5.6049 0.0000
  14. ## wc vs bc 6.0729 2.0338 2.9860 0.0036
  • Bonferroni adjustment for multiple comparisons
  1. ## Bonferroni adjustment
  2. comps <- effcomp(type_eff, contr_mat, tests = TRUE)
  3. summary(comps)
  4. ##
  5. ## Call:
  6. ## effcomp(effects_obj = type_eff, lincon = contr_mat, tests = TRUE)
  7. ##
  8. ## Pairwise differences of margins.
  9. ##
  10. ## Output:
  11. ## Contrast SE t value Pr(>|t|) AdjPr(>|t|)
  12. ## prof vs bc 17.9119 3.1957 5.6049 0.0000 0.0000
  13. ## wc vs bc 6.0729 2.0338 2.9860 0.0036 0.0072
  • Scheffe adjustment for multiple comparisons
  1. ## Scheffe's adjustment
  2. comps <- effcomp(type_eff, contr_mat, tests = TRUE, adjmethod = "scheffe")
  3. summary(comps)
  4. ##
  5. ## Call:
  6. ## effcomp(effects_obj = type_eff, lincon = contr_mat, tests = TRUE,
  7. ## adjmethod = "scheffe")
  8. ##
  9. ## Pairwise differences of margins.
  10. ##
  11. ## Output:
  12. ## Contrast SE t value Pr(>|t|) AdjPr(>|t|)
  13. ## prof vs bc 17.9119 3.1957 5.6049 0.0000 0.0000
  14. ## wc vs bc 6.0729 2.0338 2.9860 0.0036 0.0142

Contrasts of margins, two or more variables

effcomp can be used to construct contrasts of margins computed over
multiple variables.

Here, we compute margins over type and educ:

  1. type_educ <- Effect(c("type","educ"), fit_lm) # compute effects
  2. as.data.frame(type_educ) # print effects
  3. ## type educ fit se lower upper
  4. ## 1 bc 12 37.65509 1.225055 35.22203 40.08816
  5. ## 2 prof 12 55.56698 2.856968 49.89280 61.24117
  6. ## 3 wc 12 43.72801 1.721530 40.30890 47.14712
  7. ## 4 bc 13 NA NA NA NA
  8. ## 5 prof 13 67.99778 4.297428 59.46272 76.53284
  9. ## 6 wc 13 56.15881 4.188202 47.84068 64.47694
  10. ## 7 bc 14 NA NA NA NA
  11. ## 8 prof 14 64.80480 1.858274 61.11411 68.49549
  12. ## 9 wc 14 NA NA NA NA

To obtain the contrasts of these margins, we use

  1. comps <- effcomp(type_educ, all = TRUE) # compute all contrasts
  2. summary(comps)
  3. ##
  4. ## Call:
  5. ## effcomp(effects_obj = type_educ, all = TRUE)
  6. ##
  7. ## Pairwise differences of margins.
  8. ##
  9. ## Output:
  10. ## Contrast SE
  11. ## prof-12 vs bc-12 17.9119 3.1957
  12. ## wc-12 vs bc-12 6.0729 2.0338
  13. ## prof-13 vs bc-12 30.3427 4.5478
  14. ## wc-13 vs bc-12 18.5037 4.3448
  15. ## prof-14 vs bc-12 27.1497 2.3585
  16. ## wc-12 vs prof-12 -11.8390 3.3361
  17. ## prof-13 vs prof-12 12.4308 4.2291
  18. ## wc-13 vs prof-12 0.5918 5.7695
  19. ## prof-14 vs prof-12 9.2378 3.2140
  20. ## prof-13 vs wc-12 24.2698 4.9743
  21. ## wc-13 vs wc-12 12.4308 4.2291
  22. ## prof-14 vs wc-12 21.0768 2.6803
  23. ## wc-13 vs prof-13 -11.8390 3.3361
  24. ## prof-14 vs prof-13 -3.1930 4.5002
  25. ## prof-14 vs wc-13 8.6460 4.6236

If we need to test for significance of the differences, we run:

  • No adjustment for multiple comparisons
  1. ## Compute statistical tests, no adjustment for mulitple comparisons
  2. comps <- effcomp(type_educ, all = TRUE, tests = TRUE, adjmethod = "none")
  3. summary(comps)
  4. ##
  5. ## Call:
  6. ## effcomp(effects_obj = type_educ, all = TRUE, tests = TRUE, adjmethod = "none")
  7. ##
  8. ## Pairwise differences of margins.
  9. ##
  10. ## Output:
  11. ## Contrast SE t value Pr(>|t|)
  12. ## prof-12 vs bc-12 17.9119 3.1957 5.6049 0.0000
  13. ## wc-12 vs bc-12 6.0729 2.0338 2.9860 0.0036
  14. ## prof-13 vs bc-12 30.3427 4.5478 6.6720 0.0000
  15. ## wc-13 vs bc-12 18.5037 4.3448 4.2588 0.0000
  16. ## prof-14 vs bc-12 27.1497 2.3585 11.5113 0.0000
  17. ## wc-12 vs prof-12 -11.8390 3.3361 -3.5487 0.0006
  18. ## prof-13 vs prof-12 12.4308 4.2291 2.9393 0.0042
  19. ## wc-13 vs prof-12 0.5918 5.7695 0.1026 0.9185
  20. ## prof-14 vs prof-12 9.2378 3.2140 2.8743 0.0050
  21. ## prof-13 vs wc-12 24.2698 4.9743 4.8791 0.0000
  22. ## wc-13 vs wc-12 12.4308 4.2291 2.9393 0.0042
  23. ## prof-14 vs wc-12 21.0768 2.6803 7.8635 0.0000
  24. ## wc-13 vs prof-13 -11.8390 3.3361 -3.5487 0.0006
  25. ## prof-14 vs prof-13 -3.1930 4.5002 -0.7095 0.4798
  26. ## prof-14 vs wc-13 8.6460 4.6236 1.8700 0.0647
  • Bonferroni adjustment for multiple comparisons
  1. ## Bonferroni adjustment for multimple comparisons
  2. comps <- effcomp(type_educ, all = TRUE, tests = TRUE)
  3. summary(comps)
  4. ##
  5. ## Call:
  6. ## effcomp(effects_obj = type_educ, all = TRUE, tests = TRUE)
  7. ##
  8. ## Pairwise differences of margins.
  9. ##
  10. ## Output:
  11. ## Contrast SE t value Pr(>|t|) AdjPr(>|t|)
  12. ## prof-12 vs bc-12 17.9119 3.1957 5.6049 0.0000 0.0000
  13. ## wc-12 vs bc-12 6.0729 2.0338 2.9860 0.0036 0.0543
  14. ## prof-13 vs bc-12 30.3427 4.5478 6.6720 0.0000 0.0000
  15. ## wc-13 vs bc-12 18.5037 4.3448 4.2588 0.0000 0.0007
  16. ## prof-14 vs bc-12 27.1497 2.3585 11.5113 0.0000 0.0000
  17. ## wc-12 vs prof-12 -11.8390 3.3361 -3.5487 0.0006 0.0092
  18. ## prof-13 vs prof-12 12.4308 4.2291 2.9393 0.0042 0.0624
  19. ## wc-13 vs prof-12 0.5918 5.7695 0.1026 0.9185 1.0000
  20. ## prof-14 vs prof-12 9.2378 3.2140 2.8743 0.0050 0.0754
  21. ## prof-13 vs wc-12 24.2698 4.9743 4.8791 0.0000 0.0001
  22. ## wc-13 vs wc-12 12.4308 4.2291 2.9393 0.0042 0.0624
  23. ## prof-14 vs wc-12 21.0768 2.6803 7.8635 0.0000 0.0000
  24. ## wc-13 vs prof-13 -11.8390 3.3361 -3.5487 0.0006 0.0092
  25. ## prof-14 vs prof-13 -3.1930 4.5002 -0.7095 0.4798 1.0000
  26. ## prof-14 vs wc-13 8.6460 4.6236 1.8700 0.0647 0.9701
  • Scheffe adjustment for multiple comparisons
  1. ## Scheffe's adjustment for multimple comparisons
  2. comps <- effcomp(type_educ, all = TRUE, tests = TRUE, adjmethod = "scheffe")
  3. summary(comps)
  4. ##
  5. ## Call:
  6. ## effcomp(effects_obj = type_educ, all = TRUE, tests = TRUE, adjmethod = "scheffe")
  7. ##
  8. ## Pairwise differences of margins.
  9. ##
  10. ## Output:
  11. ## Contrast SE t value Pr(>|t|) AdjPr(>|t|)
  12. ## prof-12 vs bc-12 17.9119 3.1957 5.6049 0.0000 0.0005
  13. ## wc-12 vs bc-12 6.0729 2.0338 2.9860 0.0036 0.3609
  14. ## prof-13 vs bc-12 30.3427 4.5478 6.6720 0.0000 0.0000
  15. ## wc-13 vs bc-12 18.5037 4.3448 4.2588 0.0000 0.0293
  16. ## prof-14 vs bc-12 27.1497 2.3585 11.5113 0.0000 0.0000
  17. ## wc-12 vs prof-12 -11.8390 3.3361 -3.5487 0.0006 0.1433
  18. ## prof-13 vs prof-12 12.4308 4.2291 2.9393 0.0042 0.3842
  19. ## wc-13 vs prof-12 0.5918 5.7695 0.1026 0.9185 1.0000
  20. ## prof-14 vs prof-12 9.2378 3.2140 2.8743 0.0050 0.4175
  21. ## prof-13 vs wc-12 24.2698 4.9743 4.8791 0.0000 0.0052
  22. ## wc-13 vs wc-12 12.4308 4.2291 2.9393 0.0042 0.3842
  23. ## prof-14 vs wc-12 21.0768 2.6803 7.8635 0.0000 0.0000
  24. ## wc-13 vs prof-13 -11.8390 3.3361 -3.5487 0.0006 0.1433
  25. ## prof-14 vs prof-13 -3.1930 4.5002 -0.7095 0.4798 0.9998
  26. ## prof-14 vs wc-13 8.6460 4.6236 1.8700 0.0647 0.8959

As before, effcomp accepts a matrix of user-specified contrasts. We
first create the matrix of contrasts

  1. contr_mat <- matrix(c(-1, 1, 0, 0, 0, 0, 0, 0, 0,
  2. 0,-1, 0, 0, 0, 0, 0, 1, 0,
  3. 0, 0,-1, 0, 0, 1, 0, 0, 0),
  4. nrow = 3, byrow = TRUE)

and then feed it to effcomp:

  1. comps <- effcomp(type_educ, contr_mat) # compute all contrasts
  2. summary(comps)
  3. ##
  4. ## Call:
  5. ## effcomp(effects_obj = type_educ, lincon = contr_mat)
  6. ##
  7. ## Pairwise differences of margins.
  8. ##
  9. ## Output:
  10. ## Contrast SE
  11. ## prof-12 vs bc-12 17.9119 3.1957
  12. ## prof-14 vs prof-12 9.2378 3.2140
  13. ## wc-13 vs wc-12 12.4308 4.2291

Again, to test the differences (and adjust for multiple comparisons
using Scheffe’s method) we run

  1. comps <- effcomp(type_educ, contr_mat, tests = TRUE, adjmethod = "scheffe")
  2. summary(comps)
  3. ##
  4. ## Call:
  5. ## effcomp(effects_obj = type_educ, lincon = contr_mat, tests = TRUE,
  6. ## adjmethod = "scheffe")
  7. ##
  8. ## Pairwise differences of margins.
  9. ##
  10. ## Output:
  11. ## Contrast SE t value Pr(>|t|) AdjPr(>|t|)
  12. ## prof-12 vs bc-12 17.9119 3.1957 5.6049 0.0000 0.0000
  13. ## prof-14 vs prof-12 9.2378 3.2140 2.8743 0.0050 0.0470
  14. ## wc-13 vs wc-12 12.4308 4.2291 2.9393 0.0042 0.0402