这有效:
a <- c(56.60, 37.56, 15.80, 27.65, 9.20, 5.05, 3.54)
b <- c(23.18, 13.49, 10.45, 7.24, 5.44, 4.19, 3.38)
rsq <- function(c) {
x <- log(a)
y <- log((c*(a/b))-1)
stopifnot(all(is.finite(y)))
summary(lm(y ~ x))$r.squared
}
optimise(rsq, maximum = TRUE, interval=c(0.8, 3))
.
# > optimise(rsq, maximum = TRUE, interval=c(0.8, 3))
# $maximum
# [1] 1.082352
#
# $objective
# [1] 0.8093781
你也可以有一个不错的情节:
plot(Vectorize(rsq), .8, 3)
grid()
要为观察设置条件,您可以执行
rsq <- function(c) {
xy <- data.frame(a=a, y=(c*(a/b))-1)
summary(lm(log(y) ~ log(a), data=subset(xy, y>0)))$r.squared
}
optimise(rsq, maximum = TRUE, interval=c(0.1, 3))
...以及有趣的情节:
plot(Vectorize(rsq), .3, 1.5)
grid()
rsq(0.4)