次のデータがあります。
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 で適切に動作させるにはどうすればよいですか? それとももっと良い方法がありますか?