Gary King Homepage Previous: Using WhatIf with Zelig Up: Examples Next: Demos and Data Sets

Using WhatIf with Other R Model Output Objects

Suppose that instead of using Zelig, we use the function lm to fit a linear model to the same hypothetical data by typing:

  > lm.out <- lm(t ~ x + y, data = sqdata)
In this case, we could then use WHATIF to evaluate a counterfactual as follows:
  > summary(whatif(data = lm.out, cfact = data.frame(x = 2, y = 3)))
As with zelig output objects, intercepts are dropped from the observed covariate data sets extracted in this manner. Unlike with Zelig, however, counterfactuals are not generated automatically by lm; hence, the counterfactuals that you supply to whatif should not include an intercept. The parameter formula can be used to drop, select, and transform the variables in data and cfact when data is a R model or zelig output object in the same way that it can be used when data is a matrix or data frame. For example, to drop the variable x, we type:
  > summary(whatif(~ y, data = lm.out, cfact = data.frame(x = 2, y = 3)))
or more simply and equivalently:
  > summary(whatif(~ y, data = lm.out, cfact = data.frame(y = 3)))
If instead we decide to run the test using the square of x, we type:
  > summary(whatif(~ I(x^2) + y, data = lm.out, cfact = data.frame(x = 2, y = 3)))
following standard R conventions for formulas.



Gary King 2010-08-12