[R] Odp: R: Help with factors
Petr PIKAL
petr.pikal at precheza.cz
Wed May 14 14:38:02 CEST 2008
Hi
so basically you want to
1. select one city
2. compute a correlation coeficient between value V for males and
females
3. repeat this for each city
As I am not an expert in statistics I could be off track but maybe
cor(do.call(cbind,split(data$V, interaction(data$city, data$sex))))
do what you want.
check how are data organised after do.call.
AFAIK also lme models from nlme package maybe can do what you want. So it
could be worth to check.
Regards
Petr
petr.pikal at precheza.cz
724008364, 581252140, 581252257
Serguei Kaniovski <Serguei.Kaniovski at wifo.ac.at> napsal dne 14.05.2008
14:12:27:
> Hi Petr!
>
> I think this is not quite what I was looking for but close. I wanted the
> correlation coefficient between female and male in each city. The
command:
>
> > lapply(split(data[,c(4,2)], interaction(data$city, data$sex)),
> function(x)
> > summary(lm(x))$r.sq)
>
> ,as I understand, produces the autocorrelations separately for females
and
> males.
>
> Serguei
> ________________________________________
> Austrian Institute of Economic Research (WIFO)
>
> P.O.Box 91 Tel.: +43-1-7982601-231
> 1103 Vienna, Austria Fax: +43-1-7989386
>
> Mail: Serguei.Kaniovski at wifo.ac.at
> http://www.wifo.ac.at/Serguei.Kaniovski
>
> Petr PIKAL <petr.pikal at precheza.cz> schrieb am 14.05.2008 13:23:34:
>
> > Hi
> >
> > I am not sure what you want to do as you speak about using different
> > function for different part of your data.
> >
> >
> > > head(data)
> > city year sex V
> > 1 1 1975 F 25.3044
> > 2 1 1975 M 16.5711
> > 3 1 1976 M 16.6072
> > 4 1 1976 F 24.2841
> > 5 1 1977 M 14.8838
> > 6 1 1977 F 24.8124
> >
> > If it was only correlation coefficients you could use lapply split
> > construction like
> >
> > lapply(split(data[,c(4,2)], interaction(data$city, data$sex)),
> function(x)
> > summary(lm(x))$r.sq)
> >
> > $`1.M`
> > [1] 0.923669
> >
> > $`2.M`
> > [1] 0.5151131
> >
> > $`1.F`
> > [1] 0.823817
> >
> > $`2.F`
> > [1] 0.7483883
> >
> > or
> >
> > > lapply(split(data[,c(4,2)], interaction(data$city, data$sex)), cor)
> > $`1.M`
> > V year
> > V 1.000000 -0.961077
> > year -0.961077 1.000000
> >
> > $`2.M`
> > V year
> > V 1.0000000 -0.7177138
> > year -0.7177138 1.0000000
> >
> > Regards
> >
> > Petr Pikal
> > petr.pikal at precheza.cz
> > 724008364, 581252140, 581252257
> >
> >
> > r-help-bounces at r-project.org napsal dne 14.05.2008 11:46:02:
> >
> > > Hallo All,
> > >
> > > I have difficulties understanding how factors work in R. Suppose a
> have
> > > data in the panel form below. I would to compute a correlation
> > coefficient
> > > (actually apply a different function of two time series) in the V
> > variable
> > > between members of the two sexes in each city over time. How can
this
> be
> >
> > > done?
> > >
> > > Thank you in advance,
> > > Serguei
> > >
> > > city, year, sex, V
> > > 1, 1975, 1, 25.3044
> > > 1, 1975, 0, 16.5711
> > > 1, 1976, 0, 16.6072
> > > 1, 1976, 1, 24.2841
> > > 1, 1977, 0, 14.8838
> > > 1, 1977, 1, 24.8124
> > > 1, 1978, 1, 23.0570
> > > 1, 1978, 0, 14.5627
> > > 1, 1979, 1, 21.2071
> > > 1, 1979, 0, 13.5277
> > > 2, 1975, 1, 62.4457
> > > 2, 1975, 0, 26.9745
> > > 2, 1976, 1, 67.3025
> > > 2, 1976, 0, 31.4600
> > > 2, 1977, 1, 53.0577
> > > 2, 1977, 0, 25.1941
> > > 2, 1978, 0, 23.3694
> > > 2, 1978, 1, 40.1452
> > > 2, 1979, 1, 44.5686
> > > 2, 1979, 0, 23.4042
> > >
> > > ______________________________________________
> > > 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.
> >
>
>
More information about the R-help
mailing list