r - Calculating SD of compass directions using Circular -
my goal calculate mean , standard deviation series of compass degrees. since may cross 360/ 0 mark, can't use standard mean or sd calculation.
i've been using circular packing in r, seems give me correct means (though negative values used when crossing 360 mark opposed positive). sd values way small. thoughts on might wrong, or better way calculate mean , sd compass directions?
the below code attempt test calculations of mean , sd on various compass directions, , compare standard mean , sd calculation (they should agree unless cross 360/ 0 mark)
library(circular) tester<-c(20,40,60) tester<-c(340,360,20) tester<-c(340,0,20) tester<-c(300,320,340) tester<-c(160,180,200) tocirc<- circular(tester, type="angles", units="degrees",rotation="clock") mean(tocirc) sd(tocirc) mean(tester) sd(tester)
when load circular
, has separate sd
function calculates standard deviation circular data differently.
#data tester<-c(160,180,200) tocirc<- circular(tester, type="angles", units="degrees",rotation="clock") sd(tocirc) #[1] 0.2864803 #the above equivalent r = rho.circular(tocirc) #resultant length sqrt(-2*log(r)) #then sd #[1] 0.2864803
if want use sd
of base after loading circular
, use sd.default
sd.default(tocirc) #[1] 20 #which equal sd.default(tester) #[1] 20
or calculate yourself
tester<-c(340,360,20) sine = sum(sin(tester * pi/180)) #sin of each angle, convert radian first cosine = sum(cos(tester * pi/180)) #cos of each angle, convert radian first tester_mean = (atan2(sine, cosine) * 180/pi) %% 360 mu = (tester - tester_mean + 180) %% 360 - 180 #difference of each angle mean tester_sd = sqrt(sum(mu^2)/(length(tester) - 1)) #standard deviation tester_mean #[1] 0 tester_sd #[1] 20
Comments
Post a Comment