我正在尝试使用R中的optim()函数来估计球体上点分布的中值方向,但没有成功。也许问题来自我的数据。这是一个例子:
x=c(0.6896091, 0.6213466, 0.6841073, 0.6172717, 0.6539712, 0.6791404,
0.7002285, 0.6712104, 0.6561555, 0.7016914)
y=c(0.030166968, 0.006057147, 0.028140402, 0.028727403, 0.002019733,
0.036516142, 0.015386166, 0.064270094, 0.070949828, 0.059841455)
z=c(0.7235532, 0.7835125, 0.7288383, 0.7862254, 0.7565168, 0.7330995,
0.7137530, 0.7384754, 0.7512830, 0.7099635)
data1=data.frame(x,y,z)
fn <- function(par) {
sum(acos(par[1]*data1[,1]+par[2]*data1[,2]+par[,3*data1[,3]]))
}
optim(c(mean(x),mean(y),mean(z)), fn)
“无法在初始参数下评估函数
另外:警告信息: 在acos(par [1] * data1 [,1] + par [2] * data1 [,2] + par [3] * data1 [,: NaNs产生了“
而如果我只是执行
sum(acos(par[1]*data1[,1]+par[2]*data1[,2]+par[3]*data1[,3]))
我有一个价值。
答案 0 :(得分:1)
我认为您的意思是par[3]*data1[,3]
而不是par[,3*data1[,3]]
> fn <- function(par, data1) {
sum(acos(par[1]*data1[,1]+par[2]*data1[,2]+par[3]*data1[,3]))
}
>
> optim(par=c(mean(x),mean(y),mean(z)), fn=fn, data1=data1)
$par
[1] 0.67003813 0.04792312 0.74096364
$value
[1] 0.3578782
$counts
function gradient
448 NA
$convergence
[1] 0
$message
NULL
There were 50 or more warnings (use warnings() to see the first 50)