gpt4 book ai didi

r - purrr::accumulate() 在两个累积变量上,而不仅仅是 1

转载 作者:行者123 更新时间:2023-12-04 07:25:40 25 4
gpt4 key购买 nike

我有一个模型作为预测器具有先前的预测。例如target ~ lag(target prediction)使用 purrr::accumulate 我可以编写一个自定义函数来预测。一些愚蠢的数据和一个愚蠢的模型的例子说明:

 ### A model that uses a lag prediction as a predictor using purrr::accumulate() ###
my_diamonds <- diamonds %>%
group_by(cut) %>%
mutate(cumprice = cumsum(price)) %>% # cumulative within groups
mutate(lag_cumprice = lag(cumprice)) %>%
mutate(InitialValue = min(cumprice)) %>%
filter(!is.na(lag_cumprice)) %>%
select(cut, cumprice, lag_cumprice, x, InitialValue)

silly_model <- glm(formula = cumprice ~ x + lag_cumprice, family = 'poisson', data = my_diamonds)
该模型使用前一个预测作为下一个预测的输入。我能够编写一个自定义函数来改变预测:
# when predicting won't have lag_cumprice, instead the result of the previous pediction should be an input to the model:
accPrice <- function(mod, acc, cur) {

db=cur_data_all() # grouped data segment
x = db$x[cur] # cur is the current row in the data, use it to get 'this' iterations value of x

total_exponent <- mod$coefficients['(Intercept)'] +
(mod$coefficients['x'] * x) +
(mod$coefficients['lag_cumprice'] * acc) # acc is the accumulated prediction for cumprice
}

# now predict
my_diamonds <- my_diamonds %>%
mutate(predicted = accumulate(.x = row_number()[-1], .init = InitialValue %>% unique, .f = accPrice, mod = silly_model))
到现在为止还挺好。在这个例子中,我使用了之前的预测 acc作为输入。
但是,我创建了一个变异模型,现在使用两个滞后变量作为预测变量:
### now a model with lag on two variables not just one ###
my_diamonds2 <- diamonds %>%
group_by(cut) %>%
mutate(cumprice = cumsum(price)) %>% # cumulative within groups
mutate(lag_cumprice = lag(cumprice)) %>%
mutate(InitialValue = min(cumprice)) %>%
mutate(rn = row_number()) %>%
mutate(cumrn = cumsum(rn)) %>%
mutate(lag_cumrn = lag(cumrn)) %>%
filter(!is.na(lag_cumprice)) %>%
select(cut, cumprice, lag_cumprice, lag_cumrn, x, InitialValue)

silly_model2 <- glm(formula = cumprice ~ x + lag_cumprice + lag_cumrn, family = 'poisson', data = my_diamonds2)

### Stuck after here ###
如何修改上面的函数 accPrice() 以累积 2 个变量,包括 lag_cumprice 和 lag_cumrn 而不是像以前那样只是 lag_cumprice?

最佳答案

我们可以给函数添加一个参数。然后,从模型中提取相应的系数并乘以它

accPrice2 <- function(mod, acc, acc2, cur) {

db=cur_data_all() # grouped data segment
x = db$x[cur] # cur is the current row in the data, use it to get 'this' iterations value of x

total_exponent <- mod$coefficients['(Intercept)'] +
(mod$coefficients['x'] * x) +
(mod$coefficients['lag_cumprice'] * acc) +
(mod$coefficients['lag_cumrn'] * acc2)
}

my_diamonds2 %>%
mutate(predicted = accumulate(.x = row_number()[-1],
.init = InitialValue %>%
unique, .f = accPrice2, mod = silly_model))

关于r - purrr::accumulate() 在两个累积变量上,而不仅仅是 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68230017/

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