[R] Partial correlations and p-values

dadrivr dadrivr at gmail.com
Thu Nov 12 14:03:09 CET 2009


The variables have the same length, but with different numbers of missing
values (NA).  As a result, the residuals calculations (xres & yres) have
different lengths, and I cannot compute the correlation between the two
(error of incompatible dimensions - see example below).  Is there a way,
when calculating residuals, to leave the NAs in the residual calculation and
output? Thanks!

x <- c(1,20,14,NA,9)
y <- c(5,6,7,9,10)
z <- c(13,NA,16,14,NA)
xres <- residuals(lm(x ~ z))
yres <- residuals(lm(y ~ z))
cor(xres, yres)
ct <- cor.test(xres, yres)
ct$estimate
ct$p.value


Ista Zahn wrote:
> 
> 1) Think about what you did wrong. It doesn't make sense to do
> correlation/regression with variables of different lengths. You can
> have missing values in one or more variables, if that's what you mean.
> Just code them NA.
> 
> 2) Just add in the predictors, e.g.
> residuals(lm(y ~ z1 + z2))
> 
> -Ista
> 
> On Wed, Nov 11, 2009 at 10:34 PM, dadrivr <dadrivr at gmail.com> wrote:
>>
>> Awesome, that's what I was looking for.  I have two additional questions:
>> (1)
>> What can I do if the variables are of different lengths? (2) How do I
>> update
>> the formula if I want to control for more than one variable.
>>
>> Let's take the following example:
>> x <- c(1,20,14,7,9)
>> y <- c(5,6,7,9,10,11)
>> z <- c(13,27,16,5,4,17,20)
>> a <- c(4,6,7,1)
>>
>> xres <- residuals(lm(x ~ z))
>> yres <- residuals(lm(y ~ z))
>> cor(xres, yres)
>> ct <- cor.test(xres, yres)
>> ct$estimate
>> ct$p.value
>>
>> How do I update the above formula to:
>> (1) take into account that the variables are of different lengths?  I get
>> an
>> error when calculating the residuals.
>> (2) control for z and a (i.e., more than one variable)?
>>
>> Thanks so much for your help.
>>
>>
>> Peter Ehlers wrote:
>>>
>>>
>>> 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
>>>
>>>>
>>>>
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>>
>>
>> --
>> View this message in context:
>> http://old.nabble.com/Partial-correlations-and-p-values-tp26308463p26312873.html
>> Sent from the R help mailing list archive at Nabble.com.
>>
>> ______________________________________________
>> R-help at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
> 
> 
> 
> -- 
> Ista Zahn
> Graduate student
> University of Rochester
> Department of Clinical and Social Psychology
> http://yourpsyche.org
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 

-- 
View this message in context: http://old.nabble.com/Partial-correlations-and-p-values-tp26308463p26318276.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list