如何从多个拟合模型中获得预测

How to get predictions from multiple fitted models

我有一个包含多个样品校准曲线的数据框(信号 s 作为浓度 c 的函数):

cal <- data.frame(sample = c(rep("A", 8), rep("B", 8), rep("C", 8)),
                  c_std = rep(c(0, 1, 5, 10, 25, 50, 100, 200), 3),
                  s_std = c(40341, 24196, 13403,  6956,  3000,  1507, 312, 12,
                            40261, 24250, 13537,  6977,  2940,  1465, 304, 12,
                            40075, 24469, 13696,  7060,  2972,  1487, 307, 12))

此外,我有一个数据框,其中包含对相同样本的观察结果:

obs <- data.frame(sample = c("A", "B", "C"),
                  s_sample = c(1364, 4726, 521))

首先,我使用库 .

中的 drm 为每条校准曲线拟合模型
model <- function(df) drc::drm(c_std ~ s_std, fct = LL2.3(), data = df)

library(dplyr)

cal_models <- cal %>%
  group_by(sample) %>%
  nest() %>%
  mutate(model = map(data, model)) %>%
  unnest(sample) %>%
  distinct(sample, model)

我想从获得的模型中预测我对样本所做的观察。这就是我被困的地方。我的想法是通过样本 ID 合并包含模型和观察结果的两个对象,然后以与我过去使用地图拟合模型类似的方式应用统计包中的预测。不过,我不知道该怎么做。这就是我合并数据框的方式:

dat <- merge(cal_models, obs)

这是我从模型中获得预测的方法,只是我想对所有样本一步完成:

cal_A <- subset(cal, sample == "A")
model_A <- drc::drm(c_std ~ s_std, data = cal_A, fct = LL2.3()
predicted <- stats::predict(model_A, data.frame(obs$s_sample[1]))
predicted
cal <- data.frame(sample = c(rep("A", 8), rep("B", 8), rep("C", 8)),
                  c_std = rep(c(0, 1, 5, 10, 25, 50, 100, 200), 3),
                  s_std = c(40341, 24196, 13403,  6956,  3000,  1507, 312, 12,
                            40261, 24250, 13537,  6977,  2940,  1465, 304, 12,
                            40075, 24469, 13696,  7060,  2972,  1487, 307, 12))
obs <- data.frame(sample = c("A", "B", "C"),
                  s_std = c(1364, 4726, 521))
library(purrr)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(tidyr)
setNames(predict(drc::drm(c_std ~ s_std, fct = drc::LL2.3(), data = cal), obs), obs$sample)
#>        A        B        C 
#> 44.78515 18.20520 79.81111
cal %>%
    group_by(sample) %>%
    nest() %>%
    mutate(
        model = map(data, ~drc::drm(c_std ~ s_std, fct = drc::LL2.3(), data = .x)),
        pred = map2(model, sample, ~predict(.x, filter(obs, sample == .y)))
    ) %>%
    unnest(pred)
#> # A tibble: 3 x 4
#> # Groups:   sample [3]
#>   sample data             model   pred
#>   <chr>  <list>           <list> <dbl>
#> 1 A      <tibble [8 x 2]> <drc>   45.1
#> 2 B      <tibble [8 x 2]> <drc>   18.1
#> 3 C      <tibble [8 x 2]> <drc>   79.8

reprex package (v2.0.0)

创建于 2022-03-07

使用您的校准数据制作模型列表可能最简单:

models <- by( cal , INDICES = cal$sample , FUN = model )

然后将 predict 映射到您的 obs 数据,如下所示:

mapply( FUN = function(x, y) { predict(models[[x]], data.frame(y)) }, 
   x=obs$sample ,  
   y=obs$s_sample)

A.Prediction B.Prediction C.Prediction 
    45.09688     18.07154     79.75994 

如果您想将其添加到现有数据框中:

cbind(obs, prediction=mapply( FUN = function(x, y) { predict(models[[x]], data.frame(y)) }, 
        x=obs$sample ,  
        y=obs$s_sample))

             sample s_sample prediction
A.Prediction      A     1364   45.09688
B.Prediction      B     4726   18.07154
C.Prediction      C      521   79.75994