Linear regression with product of factor and independent variable

女生的网名这么多〃 提交于 2019-12-31 03:54:08

问题


I am try to estimate a demand model:

d_t^k = a_t - b^k p_t^k + e_t^k

The indices t are for week number, k are for product number. The demand for each product d_t^k depends on the general seasonality that is shared by all the products a_t, and is a affine function of the price of the product in that week p_t^k, plus some normal random error e_t^k.

However, if I use the following lm function call, it gives me a single coefficient b for price, when what I want is one coefficient per product b^k for price^k.

lm(demand ~ factor(week) + price, data = df)

What is the right way to express the model?

lm(demand ~ factor(week) + factor(product) * price, data = df)

I am guessing that the above would work, and it but I can't find any documentation that tells me what is going on there.

As a concrete example, I have the following code that runs, on a slightly different demand model d_t^k = a_t + a^k - b^k p_t^k + e_t^k

# Generate fake prices and sales, and estimate the coefficients of
# the demand model.

number.of.items <- 20 # Must be a multiple of 4
number.of.weeks <- 5
coeff.item.min <- 300
coeff.item.max <- 500
coeff.price.min <- 1.4
coeff.price.max <- 2
normal.sd <- 40
set.seed(200)

# Generate random coefficients for the items
coeff.item <- runif(number.of.items, coeff.item.min, coeff.item.max)
coeff.price <- runif(number.of.items, coeff.price.min, coeff.price.max)
coeff.week <- 50 * 1:number.of.weeks

# Row is item, column is week
week.id.matrix <- outer(rep(1, number.of.items), 1:number.of.weeks)
item.id.matrix <- outer(1:number.of.items, rep(1, number.of.weeks))
price.matrix <- rbind(
  outer(rep(1, number.of.items / 4), c(100, 100, 90, 90, 80)),
  outer(rep(1, number.of.items / 4), c(100, 90, 90, 80, 60)),
  outer(rep(1, number.of.items / 4), c(100, 85, 85, 60, 60)),
  outer(rep(1, number.of.items / 4), c(100, 75, 60, 45, 45))
)
coeff.week.matrix <- outer(rep(1, number.of.items), coeff.week)
coeff.price.matrix <- outer(coeff.price, rep(1, number.of.weeks))
coeff.item.matrix <- outer(coeff.item, rep(1, number.of.weeks))
sales.matrix <- coeff.week.matrix +
  coeff.item.matrix -
  coeff.price.matrix * price.matrix +
  matrix(rnorm(number.of.weeks * number.of.items, 0, normal.sd),
         number.of.items, number.of.weeks)


df <- data.frame(item = factor(as.vector(item.id.matrix)),
                 week = factor(as.vector(week.id.matrix)),
                 price = as.vector(price.matrix),
                 sales = as.vector(sales.matrix))

model <- lm(sales ~ week + item + price, data = df)
model <- lm(sales ~ week + item + factor(item) * price, data = df)

print(summary(model))

回答1:


After doing some experimentation, it seems that

lm(demand ~ factor(week) + factor(product) * price, data = df)

does work.

I don't know why I hadn't thought it would work earlier.



来源:https://stackoverflow.com/questions/16001039/linear-regression-with-product-of-factor-and-independent-variable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!