简体   繁体   English

如何按组将 2 个函数添加到回归中?

[英]How to add 2 functions into regression by group?

I have this dataset.我有这个数据集。

 dat=structure(list(sku = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
        1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), period = c("30.09.2021", 
        "14.03.2019", "01.04.2022", "18.02.2022", "07.07.2021", "09.10.2020", 
        "17.01.2019", "10.11.2020", "14.07.2021", "10.09.2019", "31.01.2019", 
        "01.07.2021", "30.09.2021", "14.03.2019", "01.04.2022", "18.02.2022", 
        "07.07.2021", "09.10.2020", "17.01.2019", "10.11.2020", "14.07.2021", 
        "10.09.2019", "31.01.2019", "01.07.2021"), hist.prices = c(3728.16, 
        34899.84, 6126, 1789.44, 18098.4, 15633.6, 26174.88, 2401.56, 
        12668.88, 239500.8, 26174.88, 5429.52, 3728.16, 34899.84, 6126, 
        1789.44, 18098.4, 15633.6, 26174.88, 2401.56, 12668.88, 239500.8, 
        26174.88, 5429.52), hist.revenue = c(178951.68, 20102307.84, 
        367560, 42946.56, 4343616, 3752064, 11307548.16, 86456.16, 2128371.84, 
        965667225.6, 11307548.16, 390925.44, 178951.68, 20102307.84, 
        367560, 42946.56, 4343616, 3752064, 11307548.16, 86456.16, 2128371.84, 
        965667225.6, 11307548.16, 390925.44), hist.demand = c(254L, 276L, 
        272L, 250L, 299L, 297L, 291L, 260L, 270L, 275L, 295L, 279L, 254L, 
        276L, 272L, 250L, 299L, 297L, 291L, 260L, 270L, 275L, 295L, 279L
        ), hist.cost = c(12572.6698, 10498.9848, 14949.392, 13160.5, 
        14557.9512, 12443.3199, 10692.3294, 10893.116, 13145.976, 10222.6025, 
        10982.9975, 13584.1752, 12572.6698, 10498.9848, 14949.392, 13160.5, 
        14557.9512, 12443.3199, 10692.3294, 10893.116, 13145.976, 10222.6025, 
        10982.9975, 13584.1752), unity.cost = c(49.4987, 38.0398, 54.961, 
        52.642, 48.6888, 41.8967, 36.7434, 41.8966, 48.6888, 37.1731, 
        37.2305, 48.6888, 49.4987, 38.0398, 54.961, 52.642, 48.6888, 
        41.8967, 36.7434, 41.8966, 48.6888, 37.1731, 37.2305, 48.6888
        ), hist.profit = c(1336L, 1592L, 1128L, 1882L, 1387L, 1818L, 
        1357L, 1087L, 1253L, 1009L, 1092L, 1804L, 1336L, 1592L, 1128L, 
        1882L, 1387L, 1818L, 1357L, 1087L, 1253L, 1009L, 1092L, 1804L
        )), class = "data.frame", row.names = c(NA, -24L))

and perform regression by groups( sku )并按组( sku )进行回归

library(dplyr)
library(nplyr)
library(tidyr)
dat %>% 
  nest(data = -sku) %>% 
  nest_summarise(data, 
   model.fit = list(lm(hist.demand ~ hist.prices)), 
   beta = model.fit[[1]]$coefficients[1], 
   alpha = model.fit[[1]]$coefficients[2],
   p.revenue = -beta/(2*alpha),
   p.profit = (alpha*unity.cost - beta)/(2*alpha),
   opt.revenue = true.revenue(p.revenue), ###########################  absent
   opt.profit = true.profit(p.profit)) %>% ########################## absent
  nest_select(data, opt.revenue, opt.profit) %>%
  unnest(data)

but when performing regression, the 2 funсtions true.revenue and true.profit must be also used or I will get the error that these functions are absent.但是在执行回归时,还必须使用true.revenuetrue.profit这两个函数,否则我会得到这些函数不存在的错误。 Here these functions这里有这些功能

true.revenue = function(p) p*(x*p + y) # Revenue 
true.profit = function(p) (p - unity.cost)*(x*p + y) # price 
  • p is a specific value of hist.prices for a specific period for example for 30.09.202 value for price =3728.16 and so on p是特定时期的hist.prices的特定值,例如 30.09.202 的值 price =3728.16 等等
  • x is intercept and y is beta coefficent which were got when perform regression hist.demand ~ hist.prices for each group ( sku ). x是截距, y是 beta 系数,这是在对每个组 ( sku ) 执行回归hist.demand ~ hist.prices时得到的。

So how can I add these 2 functions into regression to perform it by group?那么如何将这两个函数添加到回归中以按组执行呢?

You just need to parameterize your functions, true.revenue() and true.profit() .您只需要参数化您的函数true.revenue()true.profit()

true.revenue = function(p,x,y) p*(x*p + y) # Revenue 
true.profit = function(p,x,y,u) (p - u)*(x*p + y) # price 

Then, you can pass those values ( x , y , u ) to the functions, in addition to p :然后,除了p之外,您还可以将这些值( xyu )传递给函数:

dat %>% 
  nest(data = -sku) %>% 
  nest_summarise(data, 
                 model.fit = list(lm(hist.demand ~ hist.prices)), 
                 beta = model.fit[[1]]$coefficients[1], 
                 alpha = model.fit[[1]]$coefficients[2],
                 p.revenue = -beta/(2*alpha),
                 p.profit = (alpha*unity.cost - beta)/(2*alpha),
                 opt.revenue = true.revenue(p=p.revenue,x = hist.prices, y=hist.demand )
                 opt.profit = true.profit(p.profit,x = hist.prices, y=hist.demand,u=unity.cost)) %>%
  nest_select(data, opt.revenue, opt.profit) %>%
  unnest(data)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM