I'm trying to write code to calculate partial correlations (along with p-values). I'm new to R, and I don't know how to do this. I have searched and come across different functions, but I haven't been able to get any of them to work (for example, pcor and pcor.test from the ggm package). In the following example, I am trying to compute the correlation between x and y, while controlling for z (partial correlation): x <- c(1,20,14,7,9) y <- c(5,6,7,9,10) z <- c(13,27,16,5,4) What function can I append to this to find this partial correlation? Many thanks! -- View this message in context: http://old.nabble.com/Partial-correlations-and-p-values-tp26308463p26308463.html Sent from the R help mailing list archive at Nabble.com.
dadrivr wrote:> I'm trying to write code to calculate partial correlations (along with > p-values). I'm new to R, and I don't know how to do this. I have searched > and come across different functions, but I haven't been able to get any of > them to work (for example, pcor and pcor.test from the ggm package). > > In the following example, I am trying to compute the correlation between x > and y, while controlling for z (partial correlation): > > x <- c(1,20,14,7,9) > y <- c(5,6,7,9,10) > z <- c(13,27,16,5,4) > > What function can I append to this to find this partial correlation? Many > thanks!I'm not sure what you need, but does this give you what you want: xres <- residuals(lm(x ~ z)) yres <- residuals(lm(y ~ z)) cor(xres, yres) # [1] 0.9778857 or ct <- cor.test(xres, yres) ct$estimate # 0.9978857 ct$p.value # 0.003934582 -Peter Ehlers> >
you might look at partial.r in the psych package dadrivr wrote:> > I'm trying to write code to calculate partial correlations (along with > p-values). I'm new to R, and I don't know how to do this. I have > searched and come across different functions, but I haven't been able to > get any of them to work (for example, pcor and pcor.test from the ggm > package). > > In the following example, I am trying to compute the correlation between x > and y, while controlling for z (partial correlation): > > x <- c(1,20,14,7,9) > y <- c(5,6,7,9,10) > z <- c(13,27,16,5,4) > > What function can I append to this to find this partial correlation? Many > thanks! > > >-- View this message in context: http://n4.nabble.com/Partial-correlations-and-p-values-tp908641p949283.html Sent from the R help mailing list archive at Nabble.com.