Using optim () in R to Play Fisher's Spherical Data Results

I am trying to reproduce the results of "statistical analysis of spherical data." I want to calculate the spherical median (you can see http://www.jstor.org/stable/2345577 for the formula, equation 1, I don’t know how to write it right here). I am using the B1 book dataset:

lat1=c(-26.4,-32.2,-73.1,-80.2,-71.1,-58.7,-40.8,-14.9,-66.1,-1.8,-52.1,-77.3,-68.8,-68.4,
   -29.2,-78.5,-65.4,-49,-67,-56.7,-80.5,-77.7,-6.9,-59.4,-5.6,-62.6,-74.7,-65.3,-71.6,
   -23.3,-74.3,-81,-12.7,-75.4,-85.9,-84.8,-7.4,-29.8,-85.2,-53.1,-38.3,-72.7,-60.2,-63.4,
   -17.2,-81.6,-40.4,-53.6,-56.2,-75.1)

long1=c(324,163.7,51.9,140.5,267.2,32,28.1,266.3,144.3,256.2,83.2,182.1,110.4,142.2,246.3,222.6,247.7,
    65.6,282.6,56.2,108.4,266,19.1,281.7,107.4,105.3,120.2,286.6,106.4,96.5,90.2,170.9,199.4,118.6,
    63.7,74.9,93.8,72.8,113.2,51.5,146.8,103.1,33.2,154.8,89.9,295.6,41.0,59.1,35.6,70.7)

library('sphereplot')
B1=data.frame(long=long1,lat=lat1)
a=sph2car(B1$long,B1$lat)
x=a[,1]
y=a[,2]
z=a[,3]

First I check the data:

sqrt(x^2+y^2+z^2)

data1=data.frame(x,y,z)

median.direction <- function(par, data1) {
sum(acos(par[1]*data1[,1]+par[2]*data1[,2]+par[3]*data1[,3]))
}

median.direction2=optim(par=c(0,0,0), fn=median.direction, data1=data1)    
result1=car2sph(median.direction2$par[1],median.direction2$par[2],median.direction2$par[3])

result1

"For the data (Bl set) of Example 5.1, the spherical median (length 78.9 °, long 98.4 °).

I do not know where my error is:

Should I use colatitude with sph2car? Does optimization support warnings?

EDIT:

enter image description here

+4
source share
1 answer

.

-, , + 79 °, < 0. , .

-, ( ) . . , -90 °. -90 °, . , " " ( ). , , . , - , . , , , .

-, , (IMO). nloptr(...) nloptr. , , .

, 100 , 100 "".

library(sphereplot)
library(nloptr)
f <- function(par, data1) {
  sum(acos(par[1]*data1[,1]+par[2]*data1[,2]+par[3]*data1[,3]))
}
opts <- list(algorithm="NLOPT_GN_ISRES",xtol_rel=1.0e-6, maxeval=10000)
# set up the plot
rgl.sphgrid()
points3d(x,y,z, col="red",size=5)

set.seed(1)    # for reproducibility
# 100 initial estimates, randomly distributed on the sphere
N <- 100
xyz.init <- sph2car(long=sample(-180:180,N),lat=sample(-90:90,N))
get.median <- function(i) {
  md     <- nloptr(x0=xyz.init[i,],eval_f=f,
                   lb=c(-1,-1,-1), ub=c(1,1,1),
                   data1=data1, opts=opts)
  xyz    <- md$solution
  lines3d(c(0,xyz[1]),c(0,xyz[2]),c(0,xyz[3]),col="green",lwd=2)
  median <- car2sph(xyz[1],xyz[2],xyz[3])
  cat(".")     # cheap and dirty progress bar...
  return(median)
}  
sph.med  <- do.call(rbind,lapply(1:nrow(xyz.init),get.median))
colMeans(sph.med)
#       long        lat     radius 
#  92.314309 -77.361522   0.998315 

, () "". ( ).

, , 10000 , !!

+2

Source: https://habr.com/ru/post/1544243/


All Articles