[R] Within-group correlation confidence intervals

David Freedman 3.14david at gmail.com
Sun Sep 20 21:07:34 CEST 2009

you should save your 3 variables into a new *dataframe*


and then the command would be
by(d,d$sex,function(df) cor.test(df$educ,df$achiev))

but you could also just use
by(mydata,mydata$sex,function(df) cor.test(df$educ,df$achiev))

david freedman

jlwoodard wrote:
> I'm trying to obtain within-group correlations on a subset of variables. I
> first selected my variables using the following command:
> mydata$x<-mydata[c("iq","education","achievement")]
> I'd like to look at correlations among those variables separately for men
> and women. My gender variable in mydata is coded 1 (women) and 0 (men).
> I have successfully used the following to get within group correlations
> and p values:
> by(x,gender,function(x) rcorr(as.matrix(x)))
> However, I'm also interested in getting confidence intervals for the
> correlations as well, using cor.test.  I tried the following without
> success.
> by(x,gender,function(x) cor.test(as.matrix(x)))
> Even if I just use 2 variables (e.g., IQ and education), I get exactly the
> same output for men and women with this command:
> by(x,gender,function(x) cor.test(iq,education))
> I'm still in the learning stages with the by and cor.test functions, so I
> assume I'm using it incorrectly.  Is it possible to get the correlation
> confidence intervals for each group using this approach?  
> Many thanks in advance!
> John

View this message in context: http://www.nabble.com/Within-group-correlation-confidence-intervals-tp25509629p25530117.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list