我有以下数据:
data_ex <- structure(list(ID = c(493L, 493L, 493L, 493L, 493L, 493L, 493L,
493L, 494L, 494L, 494L, 494L, 494L, 494L, 494L), value.y = c(1.403198175,
1.403198175, 1.403198175, 1.403198175, 1.403198175, 1.403198175,
1.403198175, 1.403198175, 1.540408028, 1.540408028, 1.540408028,
1.540408028, 1.540408028, 1.540408028, 1.540408028), Sensor = structure(c(1L,
2L, 2L, 2L, 2L, 2L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L), .Label = c("Sat1",
"Sat2"), class = "factor"), Date = structure(c(3L, 1L, 2L, 1L,
2L, 4L, 3L, 4L, 4L, 3L, 5L, 2L, 1L, 1L, 5L), .Label = c("10-Jul",
"2-Jul", "30-Jun", "4-Jul", "9-Jul"), class = "factor"), variable = structure(c(1L,
2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 2L, 1L), .Label = c("A",
"B"), class = "factor"), value.x = c(0.514018, 1.250407631, 1.349420084,
0.629876797, 0.666055046, 1.434158327, 0.952216, 0.695925622,
0.667056075, 0.964285, 1.173076, 1.265919252, 0.658852868, 1.329348307,
0.60396)), .Names = c("ID", "value.y", "Sensor", "Date", "variable",
"value.x"), row.names = c(1L, 3L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 13L, 14L, 15L, 16L, 17L), class = "data.frame")
使用以下代码进行优化:
##Function to optimize
TestCalc <- function(p, x, y) {
x <- data_ex$value.x
y <- data_ex$value.y
sum((y - abs(log(1 - ((x - p[1]) / (p[2] - p[1]))) / 0.5))^2)
}
## Set limits for optimization
p <- c(1,1)
lower <- -3*p
upper <- 6 + lower
## Optimize
library(dfoptim)
opt = nmkb(p, TestCalc, lower=lower, upper=upper)
我更喜欢遍历整个数据集来比较使用不同因素进行优化的效果。像这样的东西:
data_optimize <- ddply(data_ex, .(Sensor, Date, variable), summarize, opt = nmkb(p, LAICalc, lower=lower, upper=upper))
如何将 x 和 y 移出函数,以便它们在 ddply 中正常工作?或者有没有更好的方法?