我正在对美国所有 3000 多个县的 COVID-19 数据进行线性回归,并且代码运行速度很慢。是否有并行化的选项?
我试过furrr::future_map()
了,但它并没有真正加快这个过程。无论有无,CPU 使用率约为 26%,furrr:future_map
并且只有一个进程在运行。
示例代码:
library(furrr)
future::plan(multisession, workers = 6)
# also tried multisession workers = 6, (runtime 8.5 min)
# also tried multicore workers = 6, (runtime 3.5 min)
# also tried multicore w/ default workers, (runtime 5.5 min)
# the other called regression functions look very similar
casesmodel <- function(tbl) {
lm(casesper100k ~ time, data = tbl)
}
uscases_twoweeks <-
casesdeaths %>%
filter(date >= twoweeksago) %>%
filter(!is.na(population)) %>%
filter(population > min_country_population) %>%
mutate(countyid = paste(county, state, sep = ", ")) %>%
arrange(countyid, date) %>%
group_by(countyid) %>%
nest() %>%
mutate(deathmodel = future_map(data, deathsmodel),
casemodel = future_map(data, casesmodel),
absdeathmodel = future_map(data, absdeathsmodel),
abscasemodel = future_map(data, abscasesmodel),
)