从存储在列中的模型预测值
Predict values from model stored in a column
我有一个大型数据集(下面的子集),我需要将模型拟合到每个数据子集(按几个因素分组:Round、Level、Temp),然后绘制模型拟合和置信区间。当模型存储到变量而不是列时,预测函数工作正常。
我可以根据因素嵌套数据并拟合每个模型,然后将其存储到列中。但我不太清楚如何获取该模型(存储在列中)并在预测函数中调用它。
此代码抛出:
Error: Problem with mutate()
column predicted
. i predicted = predict(model, data.frame(dose = Day))
. x no applicable method for 'predict' applied to an object of class "list" Run rlang::last_error()
to see
library(tidyverse)
library(drc)
b<- structure(list(Round = c("1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4"),
Temp...3 = c(10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10), Level = structure(c(1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3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L,
1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("low",
"med", "high"), class = "factor"), PerMort = c(0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.02, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0.04, 0, 0, 0, 0.02, 0, 0, 0, 0, 0, 0, 0, 0.04,
0, 0, 0, 0.02, 0, 0, 0, 0, 0.0344827586206897, 0, 0, 0.04,
0, 0, 0, 0.02, 0, 0, 0.0196078431372549, 0.0169491525423729,
0.0862068965517241, 0.0526315789473684, 0, 0.04, 0, 0, 0.0769230769230769,
0.02, 0, 0.0204081632653061, 0.137254901960784, 0.186440677966102,
0.172413793103448, 0.228070175438596, 0, 0.04, 0, 0.0196078431372549,
0.0961538461538462, 0.04, 0, 0.0204081632653061, 0.176470588235294,
0.338983050847458, 0.241379310344828, 0.350877192982456,
0, 0.04, 0, 0.0196078431372549, 0.115384615384615, 0.04,
0, 0.0204081632653061, 0.274509803921569, 0.559322033898305,
0.275862068965517, 0.456140350877193, 0, 0.06, 0, 0.0196078431372549,
0.115384615384615, 0.04, 0, 0.0204081632653061, 0.352941176470588,
0.661016949152542, 0.310344827586207, 0.473684210526316,
0.02, 0.08, 0.02, 0.0196078431372549, 0.115384615384615,
0.06, 0.0217391304347826, 0.0408163265306122, 0.411764705882353,
0.728813559322034, 0.327586206896552, 0.56140350877193, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0287769784172662, 0.0227272727272727,
0.0173913043478261, 0.018348623853211, 0.0181818181818182,
0.0260869565217391, 0, 0.0307692307692308, 0.0206896551724138,
0.0462962962962963, 0.0135135135135135, 0, 0.0359712230215827,
0.0378787878787879, 0.0260869565217391, 0.394495412844037,
0.0909090909090909, 0.0434782608695652, 0.117647058823529,
0.446153846153846, 0.324137931034483, 0.453703703703704,
0.344594594594595, 0.0101010101010101, 0.0719424460431655,
0.0757575757575758, 0.0434782608695652, 0.788990825688073,
0.472727272727273, 0.217391304347826, 0.53781512605042, 0.869230769230769,
0.868965517241379, 0.916666666666667, 0.804054054054054,
0.0303030303030303, 0.079136690647482, 0.0833333333333333,
0.0434782608695652, 0.917431192660551, 0.809090909090909,
0.539130434782609, 0.80672268907563, 0.976923076923077, 0.972413793103448,
0.990740740740741, 1, 0.0303030303030303, 0.0863309352517986,
0.0833333333333333, 0.0434782608695652, 0.954128440366973,
0.854545454545454, 0.756521739130435, 0.882352941176471,
1, 0.986206896551724, 0.990740740740741, 1, NA, NA, NA, NA,
NA, NA, NA, NA, 1, 1, 0.990740740740741, 1, 0.0303030303030303,
0.0863309352517986, 0.0909090909090909, 0.0434782608695652,
0.981651376146789, 0.945454545454545, 0.878260869565217,
0.949579831932773, 1, 1, 0.990740740740741, 1, 0.0505050505050505,
0.0935251798561151, 0.0909090909090909, 0.0434782608695652,
0.990825688073395, 0.972727272727273, 0.947826086956522,
0.991596638655462, 1, 1, 1, 1, 0.0505050505050505, 0.100719424460432,
0.0984848484848485, 0.0434782608695652, 0.990825688073395,
1, 0.982608695652174, 0.991596638655462, 1, 1, 1, 1, 0.0707070707070707,
0.129496402877698, 0.106060606060606, 0.0695652173913043,
1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0, 0.0126582278481013,
0, 0.0125, 0.0125, 0.0101010101010101, 0.0212765957446809,
0, 0, 0.0104166666666667, 0, 0, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 0.0526315789473684, 0.0632911392405063,
0.0617283950617284, 0.0625, 0.0625, 0.0505050505050505, 0.0425531914893617,
0.0104166666666667, 0.0315789473684211, 0.0104166666666667,
0.0104166666666667, 0, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 0.0789473684210526, 0.151898734177215, 0.135802469135802,
0.1, 0.0875, 0.101010101010101, 0.0851063829787234, 0.0729166666666667,
0.242105263157895, 0.177083333333333, 0.0729166666666667,
0.122448979591837, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 0.0921052631578947, 0.151898734177215, 0.148148148148148,
0.1, 0.1125, 0.171717171717172, 0.138297872340426, 0.09375,
0.505263157894737, 0.395833333333333, 0.239583333333333,
0.408163265306122, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 0.0921052631578947, 0.151898734177215, 0.148148148148148,
0.1, 0.275, 0.292929292929293, 0.223404255319149, 0.135416666666667,
0.831578947368421, 0.666666666666667, 0.5625, 0.76530612244898,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.118421052631579, 0.164556962025316,
0.172839506172839, 0.1, 0.3875, 0.343434343434343, 0.319148936170213,
0.21875, 0.905263157894737, 0.802083333333333, 0.729166666666667,
0.897959183673469, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 0.144736842105263, 0.177215189873418, 0.185185185185185,
0.1, 0.55, 0.424242424242424, 0.436170212765957, 0.270833333333333,
0.957894736842105, 0.885416666666667, 0.802083333333333,
0.928571428571429, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 0.25, 0.177215189873418, 0.197530864197531, 0.1,
0.6625, 0.565656565656566, 0.563829787234043, 0.34375, 0.968421052631579,
0.958333333333333, 0.875, 0.979591836734694), Day = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28)), row.names = c(NA, -480L), class = c("tbl_df",
"tbl", "data.frame"))
c<- b %>% filter(!is.na(Round)) %>%
dplyr::select(Round, Temp...3, Level, PerMort, Day) %>%
nest(dataset = c(-Round, -Temp...3, -Level) ) %>%
mutate(model = map(dataset, ~drc::drm(formula = .x$PerMort~.x$Day, fct = LL.3()) )) %>%
unnest(dataset) %>%
mutate(predicted = predict(model, data.frame(dose = Day )),
confidencedown = predict(model, data.frame(dose = Day ), interval ="confidence", level = 0.95)[,2],
confidenceup = predict(model, data.frame(dose = Day ), interval ="confidence", level = 0.95)[,3])
您可以使用 {broom}
包,tidy()
函数。默认情况下,使用参数 conf.int = TRUE
returns 95% 置信区间。如果要将 conf.level 更改为另一个值
,请查看功能帮助
c<- b %>% filter(!is.na(Round)) %>%
dplyr::select(Round, Temp...3, Level, PerMort, Day) %>%
nest(dataset = c(-Round, -Temp...3, -Level) ) %>%
mutate(model = map(dataset, ~drc::drm(formula = .x$PerMort~.x$Day, fct = LL.3()) ),
results = map(model,~ broom::tidy(.x,conf.int = TRUE))) %>%
unnest(results)
我有一个大型数据集(下面的子集),我需要将模型拟合到每个数据子集(按几个因素分组:Round、Level、Temp),然后绘制模型拟合和置信区间。当模型存储到变量而不是列时,预测函数工作正常。
我可以根据因素嵌套数据并拟合每个模型,然后将其存储到列中。但我不太清楚如何获取该模型(存储在列中)并在预测函数中调用它。
此代码抛出:
Error: Problem with
mutate()
columnpredicted
. ipredicted = predict(model, data.frame(dose = Day))
. x no applicable method for 'predict' applied to an object of class "list" Runrlang::last_error()
to see
library(tidyverse)
library(drc)
b<- structure(list(Round = c("1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a", "1a",
"1a", "1a", "1a", "1a", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b", "1b",
"1b", "1b", "1b", "1b", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4",
"4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4", "4"),
Temp...3 = c(10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10), Level = structure(c(1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L,
1L, 1L, 2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L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 1L, 1L,
1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("low",
"med", "high"), class = "factor"), PerMort = c(0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0.02, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0.04, 0, 0, 0, 0.02, 0, 0, 0, 0, 0, 0, 0, 0.04,
0, 0, 0, 0.02, 0, 0, 0, 0, 0.0344827586206897, 0, 0, 0.04,
0, 0, 0, 0.02, 0, 0, 0.0196078431372549, 0.0169491525423729,
0.0862068965517241, 0.0526315789473684, 0, 0.04, 0, 0, 0.0769230769230769,
0.02, 0, 0.0204081632653061, 0.137254901960784, 0.186440677966102,
0.172413793103448, 0.228070175438596, 0, 0.04, 0, 0.0196078431372549,
0.0961538461538462, 0.04, 0, 0.0204081632653061, 0.176470588235294,
0.338983050847458, 0.241379310344828, 0.350877192982456,
0, 0.04, 0, 0.0196078431372549, 0.115384615384615, 0.04,
0, 0.0204081632653061, 0.274509803921569, 0.559322033898305,
0.275862068965517, 0.456140350877193, 0, 0.06, 0, 0.0196078431372549,
0.115384615384615, 0.04, 0, 0.0204081632653061, 0.352941176470588,
0.661016949152542, 0.310344827586207, 0.473684210526316,
0.02, 0.08, 0.02, 0.0196078431372549, 0.115384615384615,
0.06, 0.0217391304347826, 0.0408163265306122, 0.411764705882353,
0.728813559322034, 0.327586206896552, 0.56140350877193, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0287769784172662, 0.0227272727272727,
0.0173913043478261, 0.018348623853211, 0.0181818181818182,
0.0260869565217391, 0, 0.0307692307692308, 0.0206896551724138,
0.0462962962962963, 0.0135135135135135, 0, 0.0359712230215827,
0.0378787878787879, 0.0260869565217391, 0.394495412844037,
0.0909090909090909, 0.0434782608695652, 0.117647058823529,
0.446153846153846, 0.324137931034483, 0.453703703703704,
0.344594594594595, 0.0101010101010101, 0.0719424460431655,
0.0757575757575758, 0.0434782608695652, 0.788990825688073,
0.472727272727273, 0.217391304347826, 0.53781512605042, 0.869230769230769,
0.868965517241379, 0.916666666666667, 0.804054054054054,
0.0303030303030303, 0.079136690647482, 0.0833333333333333,
0.0434782608695652, 0.917431192660551, 0.809090909090909,
0.539130434782609, 0.80672268907563, 0.976923076923077, 0.972413793103448,
0.990740740740741, 1, 0.0303030303030303, 0.0863309352517986,
0.0833333333333333, 0.0434782608695652, 0.954128440366973,
0.854545454545454, 0.756521739130435, 0.882352941176471,
1, 0.986206896551724, 0.990740740740741, 1, NA, NA, NA, NA,
NA, NA, NA, NA, 1, 1, 0.990740740740741, 1, 0.0303030303030303,
0.0863309352517986, 0.0909090909090909, 0.0434782608695652,
0.981651376146789, 0.945454545454545, 0.878260869565217,
0.949579831932773, 1, 1, 0.990740740740741, 1, 0.0505050505050505,
0.0935251798561151, 0.0909090909090909, 0.0434782608695652,
0.990825688073395, 0.972727272727273, 0.947826086956522,
0.991596638655462, 1, 1, 1, 1, 0.0505050505050505, 0.100719424460432,
0.0984848484848485, 0.0434782608695652, 0.990825688073395,
1, 0.982608695652174, 0.991596638655462, 1, 1, 1, 1, 0.0707070707070707,
0.129496402877698, 0.106060606060606, 0.0695652173913043,
1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0, 0.0126582278481013,
0, 0.0125, 0.0125, 0.0101010101010101, 0.0212765957446809,
0, 0, 0.0104166666666667, 0, 0, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 0.0526315789473684, 0.0632911392405063,
0.0617283950617284, 0.0625, 0.0625, 0.0505050505050505, 0.0425531914893617,
0.0104166666666667, 0.0315789473684211, 0.0104166666666667,
0.0104166666666667, 0, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 0.0789473684210526, 0.151898734177215, 0.135802469135802,
0.1, 0.0875, 0.101010101010101, 0.0851063829787234, 0.0729166666666667,
0.242105263157895, 0.177083333333333, 0.0729166666666667,
0.122448979591837, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 0.0921052631578947, 0.151898734177215, 0.148148148148148,
0.1, 0.1125, 0.171717171717172, 0.138297872340426, 0.09375,
0.505263157894737, 0.395833333333333, 0.239583333333333,
0.408163265306122, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 0.0921052631578947, 0.151898734177215, 0.148148148148148,
0.1, 0.275, 0.292929292929293, 0.223404255319149, 0.135416666666667,
0.831578947368421, 0.666666666666667, 0.5625, 0.76530612244898,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.118421052631579, 0.164556962025316,
0.172839506172839, 0.1, 0.3875, 0.343434343434343, 0.319148936170213,
0.21875, 0.905263157894737, 0.802083333333333, 0.729166666666667,
0.897959183673469, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 0.144736842105263, 0.177215189873418, 0.185185185185185,
0.1, 0.55, 0.424242424242424, 0.436170212765957, 0.270833333333333,
0.957894736842105, 0.885416666666667, 0.802083333333333,
0.928571428571429, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 0.25, 0.177215189873418, 0.197530864197531, 0.1,
0.6625, 0.565656565656566, 0.563829787234043, 0.34375, 0.968421052631579,
0.958333333333333, 0.875, 0.979591836734694), Day = c(0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 12, 12, 12, 12, 12, 12, 12, 12,
12, 12, 12, 12, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15,
15, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 21, 21,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 24, 24, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 27, 27, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3,
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 6, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 12, 12, 12,
12, 12, 12, 12, 12, 12, 12, 12, 12, 15, 15, 15, 15, 15, 15,
15, 15, 15, 15, 15, 15, 17, 17, 17, 17, 17, 17, 17, 17, 17,
17, 17, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 24, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 27, 27, 27, 27, 27, 27,
27, 27, 27, 27, 27, 27, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 6, 6, 6, 6, 6, 6, 6, 6,
6, 6, 6, 6, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9,
9, 9, 9, 9, 9, 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10,
10, 10, 10, 10, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12,
12, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 17, 17, 17,
17, 17, 17, 17, 17, 17, 17, 19, 19, 19, 19, 19, 19, 19, 19,
19, 19, 19, 19, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,
20, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 24, 24,
24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 26, 26, 26, 26, 26,
26, 26, 26, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28,
28, 28, 28, 28)), row.names = c(NA, -480L), class = c("tbl_df",
"tbl", "data.frame"))
c<- b %>% filter(!is.na(Round)) %>%
dplyr::select(Round, Temp...3, Level, PerMort, Day) %>%
nest(dataset = c(-Round, -Temp...3, -Level) ) %>%
mutate(model = map(dataset, ~drc::drm(formula = .x$PerMort~.x$Day, fct = LL.3()) )) %>%
unnest(dataset) %>%
mutate(predicted = predict(model, data.frame(dose = Day )),
confidencedown = predict(model, data.frame(dose = Day ), interval ="confidence", level = 0.95)[,2],
confidenceup = predict(model, data.frame(dose = Day ), interval ="confidence", level = 0.95)[,3])
您可以使用 {broom}
包,tidy()
函数。默认情况下,使用参数 conf.int = TRUE
returns 95% 置信区间。如果要将 conf.level 更改为另一个值
c<- b %>% filter(!is.na(Round)) %>%
dplyr::select(Round, Temp...3, Level, PerMort, Day) %>%
nest(dataset = c(-Round, -Temp...3, -Level) ) %>%
mutate(model = map(dataset, ~drc::drm(formula = .x$PerMort~.x$Day, fct = LL.3()) ),
results = map(model,~ broom::tidy(.x,conf.int = TRUE))) %>%
unnest(results)