我需要找到最佳权重(a)以最小化误差平方和。我以此为例,因为我需要处理更复杂的问题,这需要使用其他一些优化包,但是lm。我收到警告()说“在* y中。不推荐使用数组向量算法中长度为1的循环数组。请改用c()或as.vector()。”我尝试了as.vector(a * y),[1] * y,a * as.vector(y),但没有任何效果。
我该怎么做才能摆脱这条消息?
install.packages("NlcOptim")
library(NlcOptim)
x <- c(1:4)
y <- c(2,4,6,8)
objfun <- function(a) {
return(sum((x-a*y)^2))
}
x0 <- 1
solnl(x0,objfun = objfun)
这是我在输入warnings()
后看到的错误消息:
1: In a * y :
Recycling array of length 1 in array-vector arithmetic is deprecated.
Use c() or as.vector() instead.
其实我建议不采取任何措施。 R根本不喜欢将一维数组添加到数字向量中。
x <- array(1, dim = 1);
x + c(1,1)
[1] 2 2
Warning message:
In x + c(1, 1) :
Recycling array of length 1 in array-vector arithmetic is deprecated.
Use c() or as.vector() instead.
如您所见,结果是正确的,因此您可以非常安全地忽略该警告。