correlation - Why am I only getting 1 and -1 from the cor() function in R? -
i'm trying perform pearson correlation cor() function, output gives me 1 , -1, not coefficient itself. when go plot matrix corrplot(), see 1 , -1 values. how fix this? dataset can found here, , see script down below:
##must load libraries need! if have not installed packages, before start. library("corrplot") ##load in datasets d1=bpt5test ##if don't have y (i.e, want same thing in both axis), leave blank d2= ##run spearman correlation. if want pearson, change "spearman "pearson" ##if have 0s in dataset, set use = "complete.obs", if have no 0s, set use = "everything" cortest=cor(d1, use = "everything", method = "pearson") ##let's plotting! ##lots of changing can do! #method can "circle" "square" "pie" "color" #colorramppalette can changed, "blue" being negative, "white" being '0', , "red" being positive #change title whatever want #tl.col color of labels, can set anything.. default red corgraph=corrplot(cortest, method = "circle", col = colorramppalette(c("blue","white","red"))(200), title = "pearson's correlation of high-fat sugar @ 8 weeks", tl.cex = .5, tl.col = "black",diag = true, cl.ratio = 0.2)
your dataset contains 2 observations per variable. correlation between 2 variables consisting of 2 observations -1 or 1. see yourself, try running replicate(1e2, cor(rnorm(2), rnorm(2)))
calculates 100 correlations between 2 variables consisting of 2 observations. result -1 or 1.
Comments
Post a Comment