gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 04:19:36 27 4
gpt4 key购买 nike

我有这个数据集。

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

并按组执行回归(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)

但是在执行回归时,还必须使用 true.revenuetrue.profit 这两个函数,否则我会得到这些函数不存在的错误。这里有这些函数

true.revenue = function(p) p*(x*p + y) # Revenue 
true.profit = function(p) (p - unity.cost)*(x*p + y) # price
  • p 是特定时期的 hist.prices 的特定值,例如 30.09.202 value for price =3728.16 等等
  • x 是截距,y 是 beta 系数,这是在对每个组执行回归 hist.demand ~ hist.prices 时得到的 (sku).

那么如何将这 2 个函数添加到回归中以按组执行呢?

最佳答案

您只需参数化您的函数,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

然后,除了 p:

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)

关于r - 如何按组将 2 个函数添加到回归中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74760951/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com