John Kane
2007-Mar-09 19:13 UTC
[R] Applying some equations over all unique combinations of 4 variables
#I have a data set that looks like this. A bit more complicated actually with # three factor levels but these calculations need to be done on one factor at a #I then have a set of different rates that are applied #to it. #dataset cata <- c( 1,1,6,1,1,2) catb <- c( 1,2,3,4,5,6) doga <- c(3,5,3,6,4, 0) data1 <- data.frame(cata, catb, doga) rm(cata,catb,doga) data1 # start rates # names for lists fnams <- c("af", "pf", "cf", "mf") mnams <- c("am", "pm", "cm", "mm") # Current layout of the rate data frames alphahill <- list(af <- c("a1","a2","a3"), pf <- c("d1","d2","d3"), cf <- c("f1","f2"), mf <- c("h1","h2")) names(alphahill) <- fnams betahill <- list(am <- c("b1","b2","b3"), pm<- c("e1","e2","e3"), cm <- c("g1","g2"), mm <- c("j1", "j2")) names(betahill) <- mnams hilltop <- list(af <- data.frame(a1 <- 1:4 , a2 <- 2:5, a3 <- 3:6), pf <- data.frame(d1 <- 4:1, d2 <- 5:2, d3 <- 6:3), cf <- data.frame(f1 <- 1:4, f2 <- 3:6), mf <- data.frame(h1 <- 1:4, h2 <- 2:5)) hilldown <- list(am <- data.frame(b1 <- 4:1, b2 <- 5:2, b3 <- 6:3), pm <- data.frame(e1 <- 5:1, e2 <- 1:5,e3 <- 6:2), cm <- data.frame (g1 <- 5:1, g2 <- 1:5), mm <- data.frame(j1 <- 1:4, j2 <- 5:2)) names(hilltop) <- fnams names(hilldown) <- mnams for (i in 1:4) { names(hilltop[[i]]) <- alphahill[[i]] names(hilldown[[i]]) <- betahill[[i]] } rm(a1,a2,a3,b1,b2,b3,d1,d2,d3,e1,e2,e3,f1,f2,g1,g2,h1,h2,j1,j2, fnams, mnams, af, am,cf,cm,mf, mm,pf, pm) # Now that's out of the way #Assuming I am reading this problem correctly I should have #648 possible combinations for each row of data that is: #unique combinations where I need # (af*am) * (pf*pm) * (cf*cm) * mf * mm # ie (3*3) * (3* 3) * (2*2) * 2*2) # based on the idea that there are 9 unique combination for af & am and so # on. # af am # 1 a1 b1 # 2 a2 b1 # 3 a3 b1 # 4 a1 b2 # 5 a2 b2 # 6 a3 b2 # 7 a1 b3 # 8 a2 b3 # 9 a3 b3 # I have a set of equations of the form : # P1 <- af*cata + pf*catb^cf + mf*doga # S1 <- am*cata + pm*catb^cm + mm*doga #Is there any way that I can do something like this and keep track of #what condition is what since I need to be able to sum the P1s and P2, for # for each combination (or a subset of them) ? I suspect it may be a fairly # straight-forward "apply " problem but I am having a real problem with it. # I am only likely to need to report, perhaps. 15 combinations but at the # moment Idon't see any easy way to do them and doing all possible outcomes and # extracting the required ones looks like a better and safer approach if it can # be done.And will save a lot of time if we suddenly need a few new comparisons. # Any help would be greatly appreciated.
John Kane
2007-Mar-09 19:24 UTC
[R] Applying some equations over all unique combinations of 4 variables
I just realised after posting I have two vectors of the wrong length. The corrected program is: #dataset cata <- c( 1,1,6,1,1,2) catb <- c( 1,2,3,4,5,6) doga <- c(3,5,3,6,4, 0) data1 <- data.frame(cata, catb, doga) rm(cata,catb,doga) data1 # start rates # names for lists fnams <- c("af", "pf", "cf", "mf") mnams <- c("am", "pm", "cm", "mm") # Current layout of the rate data frames alphahill <- list(af <- c("a1","a2","a3"), pf <- c("d1","d2","d3"), cf <- c("f1","f2"), mf <- c("h1","h2")) names(alphahill) <- fnams betahill <- list(am <- c("b1","b2","b3"), pm<- c("e1","e2","e3"), cm <- c("g1","g2"), mm <- c("j1", "j2")) names(betahill) <- mnams hilltop <- list(af <- data.frame(a1 <- 1:4 , a2 <- 2:5, a3 <- 3:6), pf <- data.frame(d1 <- 4:1, d2 <- 5:2, d3 <- 6:3), cf <- data.frame(f1 <- 1:4, f2 <- 3:6), mf <- data.frame(h1 <- 1:4, h2 <- 3:6)) hilldown <- list(am <- data.frame(b1 <- 4:1, b2 <- 5:2, b3 <- 6:3), pm <- data.frame(e1 <- 4:1, e2 <- 1:4,e3 <- 6:3), cm <- data.frame (g1 <- 4:1, g2 <- 1:4), mm <- data.frame(j1 <- 1:4, j2 <- 4:1)) names(hilltop) <- fnams names(hilldown) <- mnams for (i in 1:4) { names(hilltop[[i]]) <- alphahill[[i]] names(hilldown[[i]]) <- betahill[[i]] } rm(a1,a2,a3,b1,b2,b3,d1,d2,d3,e1,e2,e3,f1,f2,g1,g2,h1,h2,j1,j2, fnams, mnams, af, am,cf,cm,mf, mm,pf, pm) --- John Kane <jrkrideau at yahoo.ca> wrote:> #I have a data set that looks like this. A bit > more > complicated actually with > # three factor levels but these calculations need to > be done on one factor at a > #I then have a set of different rates that are > applied > #to it. > > #dataset > cata <- c( 1,1,6,1,1,2) > catb <- c( 1,2,3,4,5,6) > doga <- c(3,5,3,6,4, 0) > > data1 <- data.frame(cata, catb, doga) > rm(cata,catb,doga) > data1 > > # start rates > # names for lists > fnams <- c("af", "pf", "cf", "mf") > mnams <- c("am", "pm", "cm", "mm") > > # Current layout of the rate data frames > alphahill <- list(af <- c("a1","a2","a3"), pf <- > c("d1","d2","d3"), > cf <- c("f1","f2"), mf <- > c("h1","h2")) > names(alphahill) <- fnams > > betahill <- list(am <- c("b1","b2","b3"), pm<- > c("e1","e2","e3"), > cm <- c("g1","g2"), mm <- c("j1", > "j2")) > names(betahill) <- mnams > > hilltop <- list(af <- data.frame(a1 <- 1:4 , a2 <- > 2:5, a3 <- 3:6), > pf <- data.frame(d1 <- 4:1, d2 <- > 5:2, > d3 <- 6:3), > cf <- data.frame(f1 <- 1:4, f2 <- > 3:6), > mf <- data.frame(h1 <- 1:4, h2 <- > 2:5)) > > hilldown <- list(am <- data.frame(b1 <- 4:1, b2 <- > 5:2, b3 <- 6:3), > pm <- data.frame(e1 <- 5:1, e2 <- > 1:5,e3 <- 6:2), > cm <- data.frame (g1 <- 5:1, g2 <- > 1:5), > mm <- data.frame(j1 <- 1:4, j2 <- > 5:2)) > names(hilltop) <- fnams > names(hilldown) <- mnams > for (i in 1:4) { > names(hilltop[[i]]) <- alphahill[[i]] > names(hilldown[[i]]) <- betahill[[i]] > } > >rm(a1,a2,a3,b1,b2,b3,d1,d2,d3,e1,e2,e3,f1,f2,g1,g2,h1,h2,j1,j2,> fnams, mnams, > af, am,cf,cm,mf, mm,pf, pm) > # Now that's out of the way > > #Assuming I am reading this problem correctly I > should > have > #648 possible combinations for each row of data > that > is: > #unique combinations where I need > # (af*am) * (pf*pm) * (cf*cm) * mf * mm > # ie (3*3) * (3* 3) * (2*2) * 2*2) > # based on the idea that there are 9 unique > combination for af & am and so > # on. > > # af am > # 1 a1 b1 > # 2 a2 b1 > # 3 a3 b1 > # 4 a1 b2 > # 5 a2 b2 > # 6 a3 b2 > # 7 a1 b3 > # 8 a2 b3 > # 9 a3 b3 > > # I have a set of equations of the form : > > # P1 <- af*cata + pf*catb^cf + mf*doga > # S1 <- am*cata + pm*catb^cm + mm*doga > > #Is there any way that I can do something like this > and keep track of > #what condition is what since I need to be able to > sum > the P1s and P2, for > # for each combination (or a subset of them) ? I > suspect it may be a fairly > # straight-forward "apply " problem but I am having > a > real problem with it. > > # I am only likely to need to report, perhaps. 15 > combinations but at the > # moment Idon't see any easy way to do them and > doing > all possible outcomes and > # extracting the required ones looks like a better > and > safer approach if it can > # be done.And will save a lot of time if we suddenly > need a few new comparisons. > > # Any help would be greatly appreciated. > > ______________________________________________ > R-help at stat.math.ethz.ch 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. >