# [R] squared coherency in R and Splus

Jacob W. Bowers jbowers at csm.berkeley.edu
Tue Nov 20 16:22:11 CET 2001

Hi All.

I have two questions about "coherency" in R versus Splus:

1) Why plot x\$coh surrounded by confidence intervals based on
sqrt(x\$coh)? Why not just plot sqrt(x\$coh) surrounded by ci's based
on sqrt(x\$coh) OR plot x\$coh surrounded by ci's based on x\$coh? [A
function I had used to plot squared coherency using Splus output
does it the second way, while R (and Bloomfield 2000) seem to plot
x\$coh surrounded by ci's based on sqrt(x\$coh).]

2) I think that spectrum() in Splus (6.0 Release 1 for Linux 2.2.12)
and spectrum() in R 1.4.0 (rev 2001-11-19) do not produce the same
numbers for their estimates of coherency although both claim to
produce "squared coherency" in their help files [and R clearly does
so on line 72 of spec.pgram()]. I ran the following test on both
setups (using Linux, kernel 2.4):

t<-1:240   #Monthly Time for 20 Years
y1<-2*cos(2*pi*t*5/240)+3*sin(2*pi*t*5/240) #Series with a 4 year cycle
y2<-.5*y1+rnorm(length(t)) #Second series
y1ts<-ts(y1,freq=12)
y2ts<-ts(y2,freq=12)
Y<-ts.union(y1ts,y2ts)
specy<-spectrum(Y,plot=FALSE,spans=c(3,3),taper=.1,demean=FALSE,detrend=FALSE)

#Write out the results to file:
#with: dput(specy\$coh,file="specycohR.r") in R
#with: dput(specy\$coh,file="specycohS.s") in S

specycohS<-dget("specycohS.s") #Get the Splus version into R.
specycohR<-specy\$coh

#I know that Splus includes freq=0 and R does not so I exclude the
#first entry in the Splus vector.

Rcompare<-sapply(list(Sversion=specycohS[-1],Rversion=specycohR),
FUN=function(x){
c(length=length(x),mean=mean(x),sd=sd(x),quantile(x))})

round(Rcompare,5)

Sversion  Rversion
length 120.00000 120.00000
mean     0.23751   0.28726
sd       0.22376   0.26523
0%       0.00061   0.00266
25%      0.07786   0.07150
50%      0.17793   0.22233
75%      0.31521   0.44021
100%     0.99619   1.22397

#I also plotted the two vectors against one another, and did not see
#any easily discernible pattern [i.e. it doesn't look like one is a
#simple function of the other.] But when I plotted both against
#x\$freq, both did get the relationship at freq=.25 [4 year cycle],
#but otherwise they looked quite different.

For now I'm going to trust the R version for both the calculation of
ci's for x\$coh AND for the calculation of x\$coh itself. But, it would
set my mind at ease to know what is going on. Does anyone know?

Thanks very much!

Jake Bowers
Dept of Political Science
UC-Berkeley

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._