admin管理员组文章数量:1410717
I'm getting inconsistent results when using cor()
function. It is easiest to demonstrate with small piece of code:
data("pbc2.id", package = "JM") # Mayo Clinic Primary Biliary Cirrhosis Data
df <- pbc2.id
vars_num1 <- c("years", "age", "serBilir", "serChol", "albumin",
"alkaline", "SGOT", "platelets", "prothrombin", "histologic",
"status2")
cor(df[vars_num1], use = "complete.obs", method="pearson") # years vs age: -0.17719866
cor(df$years, df$age, use = "complete.obs", method="pearson") # -0.1631033
Other columns do give consistent results such as serBilir
vs serChol
(0.39675890). I also coded it myself to test it:
v <- function(x,y=x) mean(x*y) - mean(x)*mean(y)
my_corr <- function(x,y) v(x,y) / sqrt(v(x) * v(y))
my_corr(df$years, df$age) # -0.1631033
So why does cor(df[vars_num1], use = "complete.obs", method="pearson")
give different results?
I'm getting inconsistent results when using cor()
function. It is easiest to demonstrate with small piece of code:
data("pbc2.id", package = "JM") # Mayo Clinic Primary Biliary Cirrhosis Data
df <- pbc2.id
vars_num1 <- c("years", "age", "serBilir", "serChol", "albumin",
"alkaline", "SGOT", "platelets", "prothrombin", "histologic",
"status2")
cor(df[vars_num1], use = "complete.obs", method="pearson") # years vs age: -0.17719866
cor(df$years, df$age, use = "complete.obs", method="pearson") # -0.1631033
Other columns do give consistent results such as serBilir
vs serChol
(0.39675890). I also coded it myself to test it:
v <- function(x,y=x) mean(x*y) - mean(x)*mean(y)
my_corr <- function(x,y) v(x,y) / sqrt(v(x) * v(y))
my_corr(df$years, df$age) # -0.1631033
So why does cor(df[vars_num1], use = "complete.obs", method="pearson")
give different results?
1 Answer
Reset to default 3I think the problem comes from your NA values. In the second case, the cor function keeps more rows than in the first case. Use na.omit
and you will see that you find the same thing.
data("pbc2.id", package = "JM") # Mayo Clinic Primary Biliary Cirrhosis Data
df <- pbc2.id
vars_num1 <- c("years", "age", "serBilir", "serChol", "albumin",
"alkaline", "SGOT", "platelets", "prothrombin", "histologic",
"status2")
df = na.omit(df)
cor(df[vars_num1], use = "complete.obs", method="pearson") # years vs age: -0.17719866
cor(df$years, df$age, use = "complete.obs", method="pearson") # -0.17719866
df[vars_num1]
本文标签: R correlation I39m getting inconsistent correlation results with cor() functionStack Overflow
版权声明:本文标题:R correlation: I'm getting inconsistent correlation results with cor() function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744801356a2625878.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
serChol
and 4 inplatelets
(check withsummary(df)
) so these will be excluded completely. The secondcor
command doesn't exclude them, so the results differ. – Edward Commented Mar 11 at 10:31use = "pairwiseplete.obs"
to get the same results for both. – Edward Commented Mar 11 at 10:34pairwiseplete.obs
is working – Hunter Commented Mar 11 at 11:26