R的Hmisc软件包的documentation(第149页)说:
spearman2计算Spearman的Rho秩相关的平方,并进行归纳,其中x可以与y非单调相关。 这是通过计算(rank(x),rank(x)^ 2)和y之间的Spearman多重方平方来完成的。
我想知道Python中是否有等效函数?或者,如果有人可以向我解释如何编写一个小函数来使用scipy.stats.spearmanr进行上述斜体字强调的计算?
答案 0 :(得分:0)
从Hnisc软件包(https://github.com/harrelfe/Hmisc/blob/master/R/biVar.s)的源代码中,以下是该函数的源代码:
spearman2.default <- function(x, y, p=1, minlev=0,
na.rm=TRUE, exclude.imputed=na.rm, ...)
{
if(p > 2)
stop('p must be 1 or 2')
y <- as.numeric(y)
if(is.character(x))
x <- factor(x)
if(na.rm) {
s <- !(is.na(x) | is.na(y))
if(exclude.imputed) {
im <- is.imputed(x) | is.imputed(y)
s <- s & !im
}
x <- x[s]; y <- y[s]
}
n <- length(x)
## If number of non-NA values is less then 3 then return a NA
## value.
if(n < 3)
return(c(rho2=NA,F=NA,df1=0,df2=n,P=NA,n=n,'Adjusted rho2'=NA))
## Find the number of unique values in x
u <- length(unique(x))
## If is a factor and unique values are greater then 2 then find the
## lm.fit.qr.bare without an intercept.
if(is.factor(x) && u > 2) {
if(minlev > 0) {
x <- combine.levels(x, minlev)
if(length(levels(x))<2) {
warning(paste('x did not have >= 2 categories with >=',
minlev,'of the observations'))
return(c(rho2=NA,F=NA,df1=0,df2=n,P=NA,n=n,'Adjusted rho2'=NA))
}
}
x <- model.matrix(~x, data=data.frame(x))
p <- ncol(x)-1
rsquare <- lm.fit.qr.bare(x, rank(y), intercept=FALSE)$rsquared
} else {
x <- as.numeric(x)
if(u < 3)
p <- 1
x <- rank(x)
rsquare <-
if(p==1)
cor(x, rank(y))^2
else {
x <- cbind(x, x^2)
lm.fit.qr.bare(x, rank(y), intercept=TRUE)$rsquared
}
}
df2 <- n-p-1
fstat <- rsquare/p/((1-rsquare)/df2)
pvalue <- 1-pf(fstat,p,df2)
rsqa <- 1 - (1 - rsquare)*(n-1)/df2
x <- c(rsquare,fstat,p,df2,pvalue,n,rsqa)
names(x) <- c("rho2","F","df1","df2","P","n","Adjusted rho2")
x
}
函数lm.fit.qr.bare
在此文件(https://github.com/harrelfe/Hmisc/blob/master/R/Misc.s)第213行中定义。
好像您还需要chol2inv
函数,请在此处查看详细信息:https://www.rdocumentation.org/packages/base/versions/3.6.1/topics/chol2inv
这将需要一些工作,但您绝对可以使用python进行翻译。祝你好运!