2

I am using R to compute the linear regression on the following model, as well as find the marginal effects of age on pizza at specific points (20,30,40,50,55).

mod6.22c <- lm(pizza ~ age + income + age*income +
                                I((age*age)*income), data = piz4)

The problem I am running into is when using the margins command, R does not see interaction terms that are inserted into the lm with I((age x age) x income). The margins command will only produce accurate average marginal effects when the interaction terms are in the form of variable1 x variable1. I also can't create a new variable in my table table$newvariable <- table$variable1^2, because the margins command won't identify newvariable as related to variable1.

This has been fine up until now, where my interaction terms have only been a quadratic, or an xy interaction, but now I am at a point where I need to calculate the average marginal effects with the interaction term AGE^2xINCOME included in the model, but the only way I can seem to get the summary lm output to be correct is by using I(age^2*(income)) or by creating a new variable in my table. As stated before, the margins command can't read I(age^2*(income)), and if I create a new variable, the margins command doesn't recognize the variables are related, and the average marginal effects produced are incorrect.

The error I am receiving:

> summary(margins(mod6.22c, at = list(age= c(20,30,40,50,55)), 
                   variables = "income"))
    Error in names(classes) <- clean_terms(names(classes)) : 
    'names' attribute [4] must be the same length as the vector [3]

I appreciate any help in advance.

Summary of data: Pizza is annual expenditure on pizza, female, hs, college and grad are dummy variables, income is in thousands of dollars per year, age is years old.

    > head(piz4)
  pizza female hs college grad income age agesq
1   109      1  0       0    0   19.5  25   625
2     0      1  0       0    0   39.0  45  2025
3     0      1  0       0    0   15.6  20   400
4   108      1  0       0    0   26.0  28   784
5   220      1  1       0    0   19.5  25   625
6   189      1  1       0    0   39.0  35  1225

Libraries used:

library(data.table)
library(dplyr)
library(margins)

tldr

This works:

mod6.22 <- lm(pizza ~ age + income + age*income, data = piz4)

**summary(margins(mod6.22, at = list(age= c(20,30,40,50,55)), variables = "income"))**

factor     age    AME     SE      z      p   lower  upper
 income 20.0000 4.5151 1.5204 2.9697 0.0030  1.5352 7.4950
 income 30.0000 3.2827 0.9049 3.6276 0.0003  1.5091 5.0563
 income 40.0000 2.0503 0.4651 4.4087 0.0000  1.1388 2.9618
 income 50.0000 0.8179 0.7100 1.1520 0.2493 -0.5736 2.2095
 income 55.0000 0.2017 0.9909 0.2036 0.8387 -1.7403 2.1438

This doesn't work:

mod6.22c <- lm(pizza ~ age + income + age*income + I((age * age)*income), data = piz4)

**summary(margins(mod6.22c, at = list(age= c(20,30,40,50,55)), variables = "income"))**
Error in names(classes) <- clean_terms(names(classes)) : 
  'names' attribute [4] must be the same length as the vector [3]

How do I get margins to read my interaction variable I((age*age)*income)?

Jess
  • 57
  • 5
  • 1
    I discovered the solution to my problem. It appears the variable I((age*age)*income) needed to be modified to I(age^2):income to produce the required results. `mod6.22d <- lm(pizza ~ age + income +age*income+ I(age^2):income, data = piz4) summary(mod6.22d) margins(mod6.22d) summary(margins(mod6.22d, at = list(age = c(20,30,40,50,55)), variables = "income"))` – Jess Feb 28 '19 at 02:23

0 Answers0