|
Hi,
I have a dataframe basically like this: > head(asturias.gen2011[,c(1,4,9:14)]) municipio total upyd psoe pp iu fac tipo 440 Allande 2031 1.44 31.10 39.75 4.01 21.62 1000-10000 443 Aller 12582 1.37 33.30 37.09 15.53 10.35 10000-50000 567 Amieva 805 1.48 32.69 37.36 6.15 20.16 <1000 849 Avilés 84202 4.15 30.26 35.49 14.37 11.80 >50000 1087 Belmonte de Miranda 1751 1.66 38.42 35.74 7.22 14.81 1000-10000 1260 Bimenes 1894 0.98 34.28 26.87 23.30 10.98 1000-10000 I want to do the following: 1. for every party (psoe, pp, etc.) I want to create a variable like this: upyd.lm.tipos, psoe.lm.tipos, etc. 2. I want to store in this variable a regression (psoe~total), but split up by tipo. I have the main idea of using dlply from the plyr vignette. But when I try to put all this in a loop I'm coming into trouble and I'm at the moment really confused how to solve this problem: I have the following function: elecregtipos <- function(y){ z<-dlply(asturias.gen2011, .(tipo), function(x) lm(x[,y]~x$edad.media)) # rsq<-function(x) summary(x)$r.squared # bcoefs<-ldply(z, function(x) c(coef(x), rsquare=rsq(x))) # return (bcoefs) return(z) } And I try to call it with: for (y in c("upyd", "psoe", "pp", "fac", "iu")) { eval(parse(text=paste(y,'.lm.tipos', '<- elecregtipos(',y,')',sep=''))) } At the moment I'm getting the error: Error en `[.data.frame`(x, , y) : objeto 'upyd' no encontrado If I call simply: elecregtipos("upyd") it works perfectly. The problem is the loop, column names, etc., but I'm really confused what I still could try, because I have already tried any possibility. Any hint? Thanks in advance. -- :: Igor Sosa Mayor :: [hidden email] :: :: GnuPG: 0x1C1E2890 :: http://www.gnupg.org/ :: :: jabberid: rogorido :: :: ______________________________________________ [hidden email] 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. |
|
I'm not sure I follow exactly what group of regression models you want to
create, but a good first step might be to use reshape so that each party's vote share goes on a different row and the vote shares are all in the same column. Then you can use plyr grouping on tipo and party to make your models... library(reshape2) library(plyr) ast <- melt(asturias.gen2011, id=c("municipio", "total", "tipo"), variable.name="party", value.name="vote") dlply(ast, .(party, tipo), lm, formula=vote~total) or along those lines. This way you don't have to mess around with pasting together expressions to eval and so on... Peter On Sun, Mar 18, 2012 at 12:59 PM, Igor Sosa Mayor < [hidden email]> wrote: > Hi, > > I have a dataframe basically like this: > > > head(asturias.gen2011[,c(1,4,9:14)]) > municipio total upyd psoe pp iu fac tipo > 440 Allande 2031 1.44 31.10 39.75 4.01 21.62 1000-10000 > 443 Aller 12582 1.37 33.30 37.09 15.53 10.35 10000-50000 > 567 Amieva 805 1.48 32.69 37.36 6.15 20.16 <1000 > 849 Avilés 84202 4.15 30.26 35.49 14.37 11.80 >50000 > 1087 Belmonte de Miranda 1751 1.66 38.42 35.74 7.22 14.81 1000-10000 > 1260 Bimenes 1894 0.98 34.28 26.87 23.30 10.98 1000-10000 > > I want to do the following: > 1. for every party (psoe, pp, etc.) I want to create a variable like > this: upyd.lm.tipos, psoe.lm.tipos, etc. > > 2. I want to store in this variable a regression (psoe~total), but > split up by tipo. > > I have the main idea of using dlply from the plyr vignette. But when I > try to put all this in a loop I'm coming into trouble and I'm at the > moment really confused how to solve this problem: > > I have the following function: > > elecregtipos <- function(y){ > z<-dlply(asturias.gen2011, .(tipo), function(x) lm(x[,y]~x$edad.media)) > # rsq<-function(x) summary(x)$r.squared > # bcoefs<-ldply(z, function(x) c(coef(x), rsquare=rsq(x))) > # return (bcoefs) > return(z) > } > > And I try to call it with: > for (y in c("upyd", "psoe", "pp", "fac", "iu")) { > eval(parse(text=paste(y,'.lm.tipos', '<- elecregtipos(',y,')',sep=''))) > } > > At the moment I'm getting the error: > Error en `[.data.frame`(x, , y) : objeto 'upyd' no encontrado > > If I call simply: > elecregtipos("upyd") > > it works perfectly. The problem is the loop, column names, etc., but I'm > really confused what I still could try, because I have already tried any > possibility. > > Any hint? > > Thanks in advance. > > > -- > :: Igor Sosa Mayor :: [hidden email] :: > :: GnuPG: 0x1C1E2890 :: http://www.gnupg.org/ :: > :: jabberid: rogorido :: :: > > ______________________________________________ > [hidden email] 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. > > ______________________________________________ [hidden email] 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. |
|
Peter:
many thanks for your help. This is basically what I wanted to do and in a much more elegant way. On Mon, Mar 19, 2012 at 03:13:40AM -0700, Peter Meilstrup wrote: > I'm not sure I follow exactly what group of regression models you want to > create, but a good first step might be to use reshape so that each party's > vote share goes on a different row and the vote shares are all in the same > column. Then you can use plyr grouping on tipo and party to make your > models... > > library(reshape2) > library(plyr) > > ast <- melt(asturias.gen2011, id=c("municipio", "total", "tipo"), > variable.name="party", value.name="vote") > > dlply(ast, .(party, tipo), lm, formula=vote~total) > > or along those lines. This way you don't have to mess around with pasting > together expressions to eval and so on... > > Peter > > On Sun, Mar 18, 2012 at 12:59 PM, Igor Sosa Mayor < > [hidden email]> wrote: > > > Hi, > > > > I have a dataframe basically like this: > > > > > head(asturias.gen2011[,c(1,4,9:14)]) > > municipio total upyd psoe pp iu fac tipo > > 440 Allande 2031 1.44 31.10 39.75 4.01 21.62 1000-10000 > > 443 Aller 12582 1.37 33.30 37.09 15.53 10.35 10000-50000 > > 567 Amieva 805 1.48 32.69 37.36 6.15 20.16 <1000 > > 849 Avilés 84202 4.15 30.26 35.49 14.37 11.80 >50000 > > 1087 Belmonte de Miranda 1751 1.66 38.42 35.74 7.22 14.81 1000-10000 > > 1260 Bimenes 1894 0.98 34.28 26.87 23.30 10.98 1000-10000 > > > > I want to do the following: > > 1. for every party (psoe, pp, etc.) I want to create a variable like > > this: upyd.lm.tipos, psoe.lm.tipos, etc. > > > > 2. I want to store in this variable a regression (psoe~total), but > > split up by tipo. > > > > I have the main idea of using dlply from the plyr vignette. But when I > > try to put all this in a loop I'm coming into trouble and I'm at the > > moment really confused how to solve this problem: > > > > I have the following function: > > > > elecregtipos <- function(y){ > > z<-dlply(asturias.gen2011, .(tipo), function(x) lm(x[,y]~x$edad.media)) > > # rsq<-function(x) summary(x)$r.squared > > # bcoefs<-ldply(z, function(x) c(coef(x), rsquare=rsq(x))) > > # return (bcoefs) > > return(z) > > } > > > > And I try to call it with: > > for (y in c("upyd", "psoe", "pp", "fac", "iu")) { > > eval(parse(text=paste(y,'.lm.tipos', '<- elecregtipos(',y,')',sep=''))) > > } > > > > At the moment I'm getting the error: > > Error en `[.data.frame`(x, , y) : objeto 'upyd' no encontrado > > > > If I call simply: > > elecregtipos("upyd") > > > > it works perfectly. The problem is the loop, column names, etc., but I'm > > really confused what I still could try, because I have already tried any > > possibility. > > > > Any hint? > > > > Thanks in advance. > > > > > > -- > > :: Igor Sosa Mayor :: [hidden email] :: > > :: GnuPG: 0x1C1E2890 :: http://www.gnupg.org/ :: > > :: jabberid: rogorido :: :: > > > > ______________________________________________ > > [hidden email] 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. > > > > :: Igor Sosa Mayor :: [hidden email] :: :: GnuPG: 0x1C1E2890 :: http://www.gnupg.org/ :: :: jabberid: rogorido :: :: ______________________________________________ [hidden email] 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. |
| Powered by Nabble | Edit this page |
