2013-04-14 58 views
1

我嘗試估計需求模型的產品:線性迴歸與因子和自變量

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

該指數t是週數,k是產品編號。對每種產品d_t^k的需求取決於所有產品a_t共享的一般季節性,並且是該產品在該周的價格p_t^k的仿射函數,加上一些正常的隨機錯誤e_t^k

但是,如果我用下面的lm函數調用,它給了我一個係數爲bprice,當我要的是每個產品b^k一個係數price^k

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

什麼是表達模型的正確方法?

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

我猜測,上面會工作,但我無法找到,告訴我什麼是對那裏發生的任何文件。

作爲具體的例子,我有一個運行,在一個稍微不同的需求模型下面的代碼 D_T^K = A_T +一個^ķ - B-1K-P_T^K + E_T-1K-

# 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)) 

回答

0

做了一些實驗後,看起來好像是

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

確實有效。

我不知道爲什麼我沒有想到它會提前工作。