0

I am using a lmer model (https://fhernanb.github.io/libro_modelos_mixtos/pac-lme4.html) to model the price elasticity of different products in different countries. After training the model with the historical data, sometimes for some of these products the elasticity is positive (by definition it should be negative or simply due to business restrictions). So I need to adjust some of the coefficients manually, only those that don't make sense. My model is:

model_str = """
                log(units)~
                log(price_usd) + (log(price_usd)|sku/country)

"""
model = lmerTest.lmer(model_str, data = df)

In this question Replace lmer coefficients in R the same question is solved, but in this case I'm using rpy2. So, I would like to know how to change the coefficients of a lmer model when using rpy2.

In order to change the coefficients with R:

library(lme4)
fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
summary(fm1)$coef
#             Estimate Std. Error   t value
#(Intercept) 251.40510   6.823773 36.842535
#Days         10.46729   1.545958  6.770744

fm1@beta[names(fixef(fm1)) == "Days"] <- 0
summary(fm1)$coef
#            Estimate Std. Error  t value
#(Intercept) 251.4051   6.823773 36.84253
#Days          0.0000   1.545958  0.00000
Slevin_42
  • 87
  • 9
  • If you're still after this, is this for a presentation purpose or re-analysis purpose? – kesh Nov 28 '22 at 17:03
  • @kesh sorry but I'm not sure if I understand your comment. I need a model able to predict units given the price of a product in a country. So I don't want to have any product with positive price elasticity. – Slevin_42 Nov 28 '22 at 22:26
  • If you're just trying to modify the output numbers for reporting, it would be the easiest to convert the `summary(fm1)$coefcoef` dataframe to Pandas dataframe, modify, and print it. – kesh Nov 28 '22 at 22:52
  • I understand but it's not the case. I need to modify the model object to predict. – Slevin_42 Nov 28 '22 at 23:11
  • 1
    Gotcha. It was a bad word choice ("re-analysis") on my part but that's what i meant (maybe 'post analysis' may be a better term?) Let me take a look at it. – kesh Nov 28 '22 at 23:17

1 Answers1

1

lmer returns a so-called RS4 class object, and its class "properties" (in Python term) are called "slots", and you access them with do_slot and do_slot_assign functions.

Here is a small example based on lme4's example:

from rpy2.robjects.packages import importr, isinstalled, PackageData
from rpy2.rinterface import FloatSexpVector

utils = importr('utils')
lme4 = importr('lme4')

sleepstudy = next(PackageData('lme4').fetch("sleepstudy").values())
formula = "Reaction ~ Days + (Days | Subject)"
fm1 = lmer(formula=formula, data=sleepstudy)

# get names of the "slots"
list(fm1.slotnames())

# prints: ['resp', 'Gp', 'call', 'frame', 'flist', 'cnms', 'lower', 'theta', 'beta', 'u', 'devcomp', 'pp', 'optinfo']

# get value of a slot
fm1.do_slot('beta')
# prints: [251.405105, 10.467286]

# to set a slot value (beta to a zero vector)
fm1.do_slot_assign('beta',FloatSexpVector([0,0]))

# check the new value
fm1.do_slot('beta')
# prints: [251.405105, 10.467286]
kesh
  • 4,515
  • 2
  • 12
  • 20
  • Thanks, it was very helpful to understand how to access these slots. Despite this, I realized that to adjust the price elasticities of a specific sku, it's necessary to change the random effect, not the beta coeficients, since beta are fix effects, and the coefficient (price elasticity) of each sku is the combination of the fix effect + random effect. While the fixed effect affects all the sku, adjusting the random effect I can focus on the desired sku. Is it possible to change the random effects? – Slevin_42 Nov 29 '22 at 11:46
  • 1
    I don't know which variables they are. IIRC, random effects are represented by mean and sd/var, so I'd suggest checking all the slot variables against the random effect parameters you get on the summary printout. Once you see the match, you can use `do_slot_assign` to modify their values. – kesh Nov 29 '22 at 14:04
  • 1
    Here is the class object documentation: https://www.rdocumentation.org/packages/lme4/versions/1.1-31/topics/merMod-class – kesh Nov 29 '22 at 14:08
  • After checking all the slots variables against the output of ranef(model) I don't see any value that matches... In the answer to this question https://stackoverflow.com/questions/39232183/how-to-modify-random-effects-in-lmer I see that maybe is not possible to modify the random effects... – Slevin_42 Dec 01 '22 at 15:22