Skip to contents

The causal_mod class acts like a vector of fitted values, but it also stores the fitted model for later predictions and summaries, and keeps track of observations that have been dropped (e.g. due to missingness) or subsetted.

Usage

causal_mod(x, idx = NULL)

new_causal_mod(x = list(), fitted = double(0), idx = seq_along(fitted))

is_causal_mod(x)

Arguments

x
  • For causal_mod() and new_causal_mod(): A fitted model object. For causal_mod() this should support the fitted() generic.

  • For is_causal_mod(): An object to test

idx

A set of indices that connect observations fed into the model with fitted values. Should contain values from 1 to the number of fitted values, and may contain NA values for observations with no corresponding fitted value (such as those with missing data). Defaults to a sequence over the fitted values, with NAs determined by na.action().

fitted

A vector of fitted values. Extracted automatically from the model object in causal_mod().

Value

  • For causal_mod() and new_causal_mod(): A causal_mod object.

  • For is_causal_mod(), a logical value.

Details

A causal_mod object can be treated and will behave as a numeric vector of fitted values. The original model object is stored in the "model" attribute, which can be accessed with get_model_fit(). The idx indices (which may have been computed automatically) are stored in the "idx" attribute, which can be accessed with get_model_idx().

A causal_mod object additionally supports forwarding generic model functions. So if you call fitted(), predict(), etc. on a causal_mod, object, you will get the same result as if you had called these functions on the original model object.

Functions

  • new_causal_mod(): Construct a causal_mod with minimal checks

  • is_causal_mod(): Return TRUE if an object is a causal_mod

Examples

m <- lm(yield ~ block + N*P*K, data=npk)
causal_mod(m)
#> <causal_mod[24]>
#>  [1] 50.89 58.33 51.83 55.06 65.1  55.7  53.33 55.67 58.99 59.02 68.42 56.66
#> [13] 57.77 48.38 46.01 48.34 54.82 48.32 51.56 47.39 57.38 60.65 53.22 54.15

d <- rbind(NA, npk)
m_mis <- lm(yield ~ block + N*P*K, data=d)
fitted(m_mis) # length doesn't match rows of `d`
#>        2        3        4        5        6        7        8        9 
#> 50.89167 58.32500 51.82500 55.05833 65.10000 55.70000 53.33333 55.66667 
#>       10       11       12       13       14       15       16       17 
#> 58.99167 59.02500 68.42500 56.65833 57.77500 48.37500 46.00833 48.34167 
#>       18       19       20       21       22       23       24       25 
#> 54.82500 48.32500 51.55833 47.39167 57.38333 60.65000 53.21667 54.15000 
causal_mod(m_mis) # NA for missing value
#> <causal_mod[25]>
#>  [1] NA    50.89 58.33 51.83 55.06 65.1  55.7  53.33 55.67 58.99 59.02 68.42
#> [13] 56.66 57.77 48.38 46.01 48.34 54.82 48.32 51.56 47.39 57.38 60.65 53.22
#> [25] 54.15
attr(causal_mod(m_mis), "idx")
#>  [1] NA  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
attr(causal_mod(m_mis), "mod")
#> 
#> Call:
#> lm(formula = yield ~ block + N * P * K, data = d)
#> 
#> Coefficients:
#> (Intercept)       block2       block3       block4       block5       block6  
#>     51.8250       3.4250       6.7500      -3.9000      -3.5000       2.3250  
#>          N1           P1           K1        N1:P1        N1:K1        P1:K1  
#>      9.8500       0.4167      -1.9167      -3.7667      -4.7000       0.5667  
#>    N1:P1:K1  
#>          NA  
#>