Hi jacaranda, Try
with(subset(mydata, Group1 == 1), cor.test(Gen, MSELEL, method = "pearson")) HTH, Jorge.- On Sun, Jun 3, 2012 at 11:51 AM, jacaranda tree <> wrote: > Hi all, > I started using R about 3 weeks ago, and now I've pretty much figured out > how to do the types of statistical modeling, graphs, tables etc. that I > frequently use (with zero background in computer languages or other > statistical packages that are similar to R like S or SAS!). So it's been a > quite rewarding process so far, and I thank you all R gurus for all your > generous help! > That being said, my question is about applying a model or an analysis to > different groups based on a grouping variable. Below is the first six rows > of my data: > > ID Group1 Group2 Mem Gen Chance MSELGM MSELVR MSELFM MSELRL MSELEL > ADOS Age > 1 1 1 1 75 50.0 50 53 > 52 62 57 56 3 25 > 2 2 1 1 75 12.5 50 46 > 48 47 52 55 2 30 > 3 3 1 1 25 37.5 50 48 > 43 52 63 63 3 24 > 4 4 1 1 25 37.5 50 51 > 62 52 59 54 0 31 > 5 5 1 1 50 87.5 50 45 > 58 42 46 43 6 31 > 6 6 1 1 100 100.0 50 45 > 80 49 69 63 1 31 > > Group1: First grouping variable > Group2: Second grouping variable > Mem: Memory trial > Gen: Generalization trial > MSEL: Mullen Scales of Early Learning (a scale measuring various skills in > little children). GM: Gross Motor Scale, VR: Visual Reception, FM: Fine > Motor, RL: receptive Language, EL: Expressive Language. > ADOS: An autism-specific measure. > > First I wanted to do correlations between Generalization (variable Gen) > and expressive language (MSELEL) for each group of Group1. For this, I used > lapply or by functions which work just fine. Here is the code with > lapply: lapply(split(mydata, mydata$Group1), function(x){cor.test(x[,5], > x[,11], method = "pearson")}) > > Then I did regression. My DV is the variable Gen, and the IV is MSELEL. > And again I wanted to do this for each group. Here is the code I came up > with for each group: > fit1<-lm(Gen~ MSELEL, data=mydata, subset=mydata$Group1==1) > > fit2<-lm(Gen~MSELEL, data=mydata, subset=mydata$Group1==2) > > This works fine for regression, but when I used the "subset" function with > the correlation (e.g. cor.test (mydata$Gen, mydata$MSELEL, > method="pearson", subset=mydata$Group1==1) , it did not work. It just did > the correlation for the entire group and then used this for both groups. I > was just curious as to why subset function works with regression, but not > with correlation. Any thoughts? > Thanks, > [[alternative HTML version deleted]] > > > ______________________________________________ > R-help@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. > > [[alternative HTML version deleted]] ______________________________________________ R-help@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.