Code Monkey home page Code Monkey logo

Comments (2)

grantmcdermott avatar grantmcdermott commented on September 28, 2024 1

Several things:

  1. Your model is misspecified. The varying slopes syntax should only appear in the fixed-effects slot, i.e. after the |.
  2. You are effectively saying that the varying the slopes syntax (| Region[MktExpenses]) is the same as a full regular interaction with both parent terms (Region * MktExpenses <=> Region + MktExpenses + Region:MktExpenses). This is not correct. Varying slopes is equivalent to a nested interaction where there is only one parent term (Region / MktExpenses <=> Region + Region:MktExpenses).

Here's proof using your demo dataset:

library(fixest)
set.seed(123)

# Number of observations
n <- 10000
n_regions<-3
# Generate fake data
data <- data.frame(
  Sales = NA,
  MktExpenses = rnorm(n, mean = 50, sd = 10),
  Region = sample(LETTERS[1:n_regions], n, replace = TRUE)
)
fe<-setNames(rnorm(n_regions, mean = 50, sd = 10), LETTERS[1:n_regions])
# Calculate sales
data$Sales <- data$MktExpenses+fe[data$Region]+(data$Region=='A')*data$MktExpenses+rnorm(n, mean = 0, sd = 2)

## these three models are all equivalent
m1 = feols(Sales ~ Region + Region:MktExpenses, data = data)
m2 = feols(Sales ~ Region/MktExpenses, data = data)
m3 = feols(Sales ~ 1 | Region[MktExpenses], data = data)

coef(m2)[4:6]
#> RegionA:MktExpenses RegionB:MktExpenses RegionC:MktExpenses 
#>           2.0005741           0.9978691           1.0029734
fixef(m3)[[2]]
#>         A         B         C 
#> 2.0005741 0.9978691 1.0029734

Created on 2024-06-14 with reprex v2.1.0

The "interaction terms" section of the introductory vignette explains this all in some detail, so I would recommend taking a look at it next: https://lrberge.github.io/fixest/articles/fixest_walkthrough.html#interaction-terms

from fixest.

lrberge avatar lrberge commented on September 28, 2024

Thanks a lot Grant for your very detailed answer!

from fixest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.