[R] car::deltaMethod() fails when a particular combination of categorical variables is not present

Michael Cohn non @end|ng |rom mcohn@net
Tue Sep 26 07:12:34 CEST 2023

I'm running a linear regression with two categorical predictors and their
interaction. One combination of levels does not occur in the data, and as
expected, no parameter is estimated for it. I now want to significance test
a particular combination of levels that does occur in the data (ie, I want
to get a confidence interval for the total prediction at given levels of
each variable).

In the past I've done this using car::deltaMethod() but in this dataset
that does not work, as shown in the example below: The regression model
gives the expected output, but deltaMethod() gives this error:

error in t(gd) %*% vcov. : non-conformable arguments

I believe this is because there is no parameter estimate for when the
predictors have the values 't1' and 'other'. In the df_fixed dataframe,
putting one person into that combination of categories causes deltaMethod()
to work as expected.

I don't know of any theoretical reason that missing one interaction
parameter estimate should prevent getting a confidence interval for a
different combination of predictors. Is there a way to use deltaMethod() or
some other function to do this without changing my data?

Thank you,

- Michael Cohn
Vote Rev (http://voterev.org)


# create dataset with outcome and two categorical predictors
outcomes <- c(91,2,60,53,38,78,48,33,97,41,64,84,64,8,66,41,52,18,57,34)
persontype <-
arm_letter <-
df <- data.frame(a = outcomes, b=persontype, c=arm_letter)

# note: there are no records with the combination 't1' + 'other'

#regression works as expected
minimal_formula <- formula("a ~ b*c")
minimal_model <- lm(minimal_formula, data=df)

#use deltaMethod() to get a prediction for individuals with the combination
'b2' and 'sent'
# deltaMethod() fails with "error in t(gd) %*% vcov. : non-conformable
deltaMethod(minimal_model, "bt2 + csent + `bt2:csent`", rhs=0)

# duplicate the dataset and change one record to be in the previously empty
df_fixed <- df
df_fixed[c(13),"c"] <- 'other'

#deltaMethod() now works
minimal_model_fixed <- lm(minimal_formula, data=df_fixed)
deltaMethod(minimal_model_fixed, "bt2 + csent + `bt2:csent`", rhs=0)

	[[alternative HTML version deleted]]

More information about the R-help mailing list