我有日期(测试):
test<-structure(list(X = c(630L, 631L, 977L, 978L), si = c(1.063166618,
1.063166618, 1.063166618, 1.063166618), ho = c(-0.071587466,
-0.071587466, -0.071587466, -0.071587466), bd = c(0.998351574,
0.999361098, 0.999978304, 0.944579005), i1 = c(0.997434318, 0.997434318,
0.997434318, 0.997434318), x1b1m = c(2.938615084, 3.220915079,
4.088527679, 1.818297207), x1b1e = c(3.561241746, 3.843541741,
4.711154342, 1.594420075), x1b0m = c(2.714737952, 2.997037947,
3.864650548, 1.594420075), x1b0e = c(2.938615084, 3.220915079,
4.088527679, 0.971793413), x2b1m = c(7.432558537, 8.66190052,
8.643460274, 6.17045182), x2b1e = c(8.055185199, 9.284527183,
9.266086936, 5.946574688), x2b0m = c(7.208681405, 8.438023388,
8.419583142, 5.946574688), x2b0e = c(7.432558537, 8.66190052,
8.643460274, 5.323948026)), .Names = c("X", "si", "ho", "bd",
"i1", "x1b1m", "x1b1e", "x1b0m", "x1b0e", "x2b1m", "x2b1e", "x2b0m",
"x2b0e"), class = "data.frame", row.names = c(NA, -4L))
test<-
X si ho bd i1 x1b1m x1b1e x1b0m x1b0e x2b1m x2b1e x2b0m x2b0e
1 630 1.063167 -0.07158747 0.9983516 0.9974343 2.938615 3.561242 2.714738 2.9386151 7.432559 8.055185 7.208681 7.432559
2 631 1.063167 -0.07158747 0.9993611 0.9974343 3.220915 3.843542 2.997038 3.2209151 8.661901 9.284527 8.438023 8.661901
3 977 1.063167 -0.07158747 0.9999783 0.9974343 4.088528 4.711154 3.864651 4.0885277 8.643460 9.266087 8.419583 8.643460
4 978 1.063167 -0.07158747 0.9445790 0.9974343 1.818297 1.594420 1.594420 0.9717934 6.170452 5.946575 5.946575 5.323948
我正在尝试使用以下代码找到积分。如果发生错误,我希望代码返回 NA ,否则返回整数值。对于第 1 行和第 4 行,它返回值,但对于第 2 和 3 行,它返回错误。不幸的是,我没有成功使用tryCatch
.
funint<- function(a,b,si,ho,bd,i1){
integrate(function(x){(1/bd)*(1/si)*dnorm((log(x)-b)/si)*pnorm(a+((ho/si)*(log(x)-b))/i1)},0,Inf)$value}
rowno<-as.list(rownames(test))
var<-c("b","e")
x1b1<-as.list(c("x1b1m","x1b1e"))
x1b0<-as.list(c("x1b0m","x1b0e"))
x2b1<-as.list(c("x2b1m","x2b1e"))
x2b0<-as.list(c("x2b0m","x2b0e"))
在以下代码中,对于每个 i(行号),积分函数 (funint) 使用列 x1b1 和 x2b1 或 x1b0 和 x2b0 中的值加上其他四个变量:si、ho、bd 和 i1。
kk<-data.frame(
tryCatch(
mapply(
function(j1b1,j2b1,j1b0,j2b0){
(sapply(rowno,
function(i){
funint(test[i,j1b1],test[i,j2b1],test[i,"si"],test[i,"ho"],test[i,"bd"],test[i,"i1"])-
funint(test[i,j1b0],test[i,j2b0],test[i,"si"],test[i,"ho"],test[i,"bd"],
test[i,"i1"])}))
},x1b1,x2b1,x1b0,x2b0),error=function(e) print("hello")))
test[i,"i1"])
}))},x1b1,x2b1,x1b0,x2b0))
names(kk)<-paste0("var")
预期输出:
kk
b e
1 601.2711 2578.3789
2 NA NA
3 NA NA
2 188.4013 354.9656
在这方面的任何帮助将不胜感激。
注意:代码有效。我唯一的问题是如何跳过错误(通过在发生错误时返回 NA )并保持代码对其他行有效。