【发布时间】:2021-12-24 10:57:45
【问题描述】:
我正在寻求一些帮助来循环我的代码或为我需要的计算创建一个函数。
我的数据框如下。除了 newdat2$time 之外,所有列在每一行中都重复相同的值,它的值每行都会发生变化:
newdat2 <- data.frame(season = rep("Summer", 31),
time = seq(0, 3, by = 0.1),
temp = rep(21.79384, 31),
last.rain.bom = rep(4.232604, 31),
rain = rep(0.916501, 31),
wind = rep("nil", 31),
cloud = rep(40.20378, 31),
abundance = rep(117.6262, 31),
site = rep("Avalon", 31))
对于这个数据框的每一行,我想完成下面的计算。此计算是计算拟合模型预测的标准误差,请参阅here。
C = c(0,0,0,0,0,0,0.0,0,0,0,0,0,0, 0, 1,0,0,0,time,21.8,4.23,0.917,0,0,0,40.2,4.78) # This represents covariate values of my fitted model. The value of time needs to change for each row of newdat2$time, all other values remain the same
s <- vcov(zib) # zib is my fitted model and this row of code is taking the variance covariance matrix of my fitted model. s is a matrix 27x27
newdat2$se <- sqrt(t(C) %*% s %*% C) # This then calculates the standard errors for my model predictions but C must change for each row of newdat2 to reflect the change in newdat2$time
例如,循环/函数完成的第一个计算是
C = c(0,0,0,0,0,0,0.0,0,0,0,0,0,0, 0, 1,0,0,0,0,21.8,4.23,0.917,0,0,0,40.2,4.78) # 0 is the first value of newdat2$time
s <- vcov(zib)
newdat2$se <- sqrt(t(C) %*% s %*% C)
循环/函数完成的第二次计算是
C = c(0,0,0,0,0,0,0.0,0,0,0,0,0,0, 0, 1,0,0,0,0.1,21.8,4.23,0.917,0,0,0,40.2,4.78) # 0.1 is the second value of newdat2$time
s <- vcov(zib)
newdat2$se <- sqrt(t(C) %*% s %*% C)
循环/函数完成的第三次计算是
C = c(0,0,0,0,0,0,0.0,0,0,0,0,0,0, 0, 1,0,0,0,0.2,21.8,4.23,0.917,0,0,0,40.2,4.78) # 0.2 is the third value of newdat2$time
s <- vcov(zib)
newdat2$se <- sqrt(t(C) %*% s %*% C)
非常感谢任何帮助循环这样的计算或创建一个可以实现这一点的函数。
【问题讨论】: