我有一个看起来像这样的缩写形式的数据集:
library(tidyverse)
dat_s<-tibble(
type=c(rep("A", 9), rep("B", 8), rep("C", 10)),
ref=c("ref3", "ref3", "ref1", "ref2", "ref2", "ref1", "ref2", "ref2", "ref2", "ref2",
"ref1", "ref2", "ref2", "ref3", "ref2", "ref3", "ref1", "ref3",
"ref2", "ref3", "ref1", "ref1", "ref3", "ref1", "ref1", "ref2", "ref2"),
info=as.character(sample(100, 27)),
liv=c(3.0e-05, 2.9e-07, 2.2e-07, 2.7e-07, 2.6e-06, 4.8e-07, 1.4e-05, 2.6e-06, 7.7e-06, 2.2e-06,
1.5e-07, 1.6e-07, 1.8e-06, 6.1e-08, 4.9e-06, 4.9e-06, 1.8e-06, 1.5e-07,
4.3e-08, 1.8e-06, 1.0e-07, 1.6e-07, 9.7e-07, 1.0e-06, 6.4e-07, 1.2e-07, 5.7e-06),
prod=c(0.00, 2, 3, 4.80, 2.10, 5.10, 0.00, 0.13, 2.00, 0.13, 0.00, 4.10, 4.60, 2.10, 0.26, 0.00,
4.60, 0.00, 4.60, 2.10, 4.80, 0.00, 0.00, 1.80, 3.60, 4.10, 0.00)
)%>%
mutate(livp1=liv+1)
我想为type
和ref
的每种组合计算剂量反应关系,进行预测以绘制曲线并计算残差。 info
列反映出我需要保留此数据框中的其他列,但在剂量反应分析中并不重要。
我首先使用函数和嵌套数据框创建模型:
dr_s<-function(df){drc::drm(data=df, prod~log(livp1), fct=drc::LL.3())}
dat_mods<-
dat_s%>%
group_by(type, ref)%>%
nest() %>%
mutate(dr_mod=map(data, dr_s))
这将创建模型并将其放入数据框中。要将add_predictions
用于类型为drm
的型号,输入必须为data.frame
(而不是tibble
)。当我尝试为每个livp1
变量添加预测时(根据下面的评论):
dat_mods%>%
mutate(mod_preds = map2(data,dr_mod,〜add_predictions(data = as.data.frame(.x),model = .y)))))>
我收到non-numeric argument to binary operator
错误消息。当info
列不是character
类时,此代码可以正常工作。但是,我需要将此信息与预测数据一起保留,并尽可能避免从数据帧中提取它。
任何指导表示赞赏!
我有一个缩写形式的数据集,看起来像这样:library(tidyverse)dat_s
[这很愚蠢,predict
类模型的drm
方法不适用于tibble
类的对象。因此,您必须将.x
转换为data.frame
。