我们有这个for loop
:
tenIDs <- unique(ten_squirrel$squirrel_id)
tenIDs
[1] 5241 3102 2271 3119 3216
#for loop to run through all the squirrels
for (i in tenIDs){
#Creating our dataframe for squirrel_id "i"
Individual_DFs <- ten_squirrel %>% filter (squirrel_id %in% i)
#Fit model for squirrel_id "i"
nls.floop <- nls(wt ~ A*atan(k*age - t0) + m,
data = Individual_DFs,
start = list(A = 102.8, k = 0.02, t0 = 0.751, m = 82.06))
#Show resulting output
print(nls.floop)
}
输出如下:
Nonlinear regression model
model: wt ~ A * atan(k * age - t0) + m
data: Individual_DFs
A k t0 m
100.69638 0.03493 1.78392 123.87479
residual sum-of-squares: 401.1
Number of iterations to convergence: 7
Achieved convergence tolerance: 5.341e-06
Nonlinear regression model
model: wt ~ A * atan(k * age - t0) + m
data: Individual_DFs
A k t0 m
140.23662 0.01953 0.54546 63.33266
residual sum-of-squares: 215.8
Number of iterations to convergence: 12
Achieved convergence tolerance: 4.367e-06
Nonlinear regression model
model: wt ~ A * atan(k * age - t0) + m
data: Individual_DFs
A k t0 m
70.76447 0.04409 2.04846 101.03060
residual sum-of-squares: 146
Number of iterations to convergence: 9
Achieved convergence tolerance: 5.725e-06
Nonlinear regression model
model: wt ~ A * atan(k * age - t0) + m
data: Individual_DFs
A k t0 m
94.35265 0.03234 1.30053 96.80194
residual sum-of-squares: 199.5
Number of iterations to convergence: 8
Achieved convergence tolerance: 7.996e-06
Nonlinear regression model
model: wt ~ A * atan(k * age - t0) + m
data: Individual_DFs
A k t0 m
75.60633 0.04844 2.06589 98.25557
residual sum-of-squares: 481.2
Number of iterations to convergence: 9
Achieved convergence tolerance: 4.24e-06
我们只展示了5个型号,但我想从每个型号中提取A
、k
、t0
和m
,这样我们就有了如下输出:
tenIDs A k t0 m
3216 75.60633 0.04844 2.06589 98.25557
3119 94.35264 0.03234 1.30053 96.80194
2271 70.76447 0.04409 2.04846 101.03060
3102 140.23656 0.01953 0.54546 63.33272
5241 100.69638 0.03493 1.78392 123.87479
如何提取每个型号的这些值沿着相应的tenIDs
?
2条答案
按热度按时间zujrkrfu1#
该模型的输出包含函数
...$m$getPars()
,产生拟合的参数。没有一个样本数据集,我无法验证,但你应该得到这个修改后的代码所需的输出:
uajslkp62#
我们最终做了一些类似于上面建议的事情。如果有人想要背景脚本,我们的答案建立在这个OP中发布的模型之上。
输出如下: