我正在尝试使用
circular
包检查角度/圆形变量和线性变量之间的相关系数,如本源中所述:https://digitalcommons.calpoly.edu/cgi/viewcontent.cgi?文章=1056&上下文=statsp。具体来说,我正在使用参数方法。
首先我创建了两个向量并将其中一个强制为圆形:
theta = c(45,36,57,89,16,39,50,48,41,46)
y = as.circular(theta,units = "degrees",
type = "angles")
x = c(10,8,8,4,6,8,9,9,9,10)
然后,我从上面链接的源的附录中复制/粘贴以下函数:
#####Function#####
cor.circular.lc = function(x,y=NULL,test =
FALSE){
### x vector or matrix of linear data
### y vector or matrix of circular data
### test if test == TRUE then a
### significance test for the correlation
### is computed
if (!is.null(y) & NROW(x) != NROW(y))
stop("x and y must have the same number
of observations")
if (is.null(y) & NCOL(x) < 2)
stop("supply both x and y or a
matrix-like x")
ncx <- NCOL(x)
ncy <- NCOL(y)
if (is.null(y)) {
ok <- complete.cases(x)
x <- x[ok, ]
}
else {
ok <- complete.cases(x, y)
if (ncx == 1) {
x <- x[ok]
}
else {
x <- x[ok, ]
}
if (ncy == 1) {
y <- y[ok]
}
else {
y <- y[ok, ]
}
}
n <- NROW(x)
if (n == 0) {
warning("No observations (at least after
removing missing values)")
return(NULL)
}
#### Converting y to radians ####
if (!is.null(y)) {
y <- conversion.circular(y, units =
"radians", zero = 0,
rotation = "counter",
modulo = "2pi")
attr(y, "class") <- attr(y, "circularp") <- NULL}
if(is.null(y)){
z = conversion.circular(x[,2], units =
"radians", zero = 0,
rotation =
"counter",
modulo = "2pi");
attr(z, "class") <- attr(z, "circularp") <- NULL;
r_xs = cor(x[,1],sin(z));
r_xc = cor(x[,1],cos(z));
r_cs = cor(cos(z),sin(z));}else{
#### calculating individual components ####
r_xs = cor(x,sin(y));
r_xc = cor(x,cos(y));
r_cs = cor(cos(y),sin(y));}
#### calculating correlation coeff linear-circular ####
cor.lc = (r_xc^2 + r_xs^2 - 2*(r_xc*r_xs*r_cs))/(1-r_cs^2);
if(test){
f.stat = (.5*(n-3)*cor.lc)/(1-cor.lc);
p.val = pf(f.stat,df1 = 2, df2=
n-3,lower.tail = FALSE);
result = list(cor = cor.lc, statistic =
f.stat, p.value = p.val);
}else{
result = list(cor = cor.lc);
}
return(result);
}
但是当我尝试使用变量运行该函数时,它不会返回具有三个值的输出,而是仅返回一个值,而忽略了 F 统计量和 p 值:
> cor.circular.lc(x,y)
$cor
[1] 0.8858006
据我所知,这是由
}else{result = list(cor = cor.lc);
行引起的,导致仅返回 cor
,而不返回 f.stat
或 p.val
。函数开头的注释 (#test if test == TRUE then a significance test for the correlation is computed
) 向我表明,由于某种原因,当前 test == FALSE
,当我查看函数的前几行时,我看到 function(x,y=NULL,test = FALSE)
,这进一步向我表明由于某种原因,y
是NULL
。我的逻辑合理吗?如果是这样,我该如何解决?如果没有,出了什么问题?我需要此测试的 F 统计量和 p 值。
(这是我第一次在这里提问,所以如果有什么我忘记包括的内容请告诉我)
function(x,y=NULL,test = FALSE)
表示 FALSE
是测试的 default 值。如果您想计算显着性检验,您应该在函数调用中包含 test = TRUE
:
>cor.circular.lc(x, y, test = TRUE)
$cor
[1] 0.8858006
$statistic
[1] 27.14815
$p.value
[1] 0.0005032954