Wow -- many thanks for the mind-*expanding* help! I'm really impressed by R's ability to handle this so concisely It's going to take me a while to get used to applying things to vectors, but the more I understand, the nicer R looks.
On Sun, Jun 22, 2008 at 6:59 PM, jim holtman <[EMAIL PROTECTED]> wrote: > This does away with the 'for' loops and uses 'expand.grid' to create > the combinations. I think I got the right variables substituted: > > my.df <- data.frame(replicate(10, round(rnorm(100, mean=3.5, sd=1)))) > var.list <- c("dv1", "dv2", "dv3", "iv1", "iv2", "iv3", "iv4", "iv5", > "intv1", "intv2") > names(my.df) <- var.list > > # I have some are DVs: > dvs <- c("dv1", "dv2", "dv3") > > # some IVs: > ivs <- c("iv1", "iv2", "iv3", "iv4", "iv5") > > # and some binary interaction variables: > intvs <- c("intv1", "intv2") > library(car) > my.df[intvs] <- lapply(my.df[intvs], function(x) > recode(x, recodes = "lo:3.5=0; 3.5:hi=1; ",as.factor.result = FALSE)) > > # now I loop through a series of interactions using the vector numbers: > # create a dataframe of values to check > xpnd <- expand.grid(dvs, ivs, intvs) # create combinations > invisible(apply(xpnd, 1, function(.row) { > jpeg(paste(paste(.row, collapse="_"),".jpg", sep='')) > > my.fit <- lm( my.df[[.row[1]]] ~ my.df[[.row[2]]] + my.df[[.row[3]]] + > my.df[[.row[2]]]:my.df[[.row[3]]]) > colors <- ifelse (my.df[[.row[3]]] == 1, "black", "grey") > plot(my.df[[.row[2]]], my.df[[.row[1]]], xlab=.row[2], > ylab=.row[1], col=colors, pch=".") > curve (cbind (1, 1, x, 1*x) %*% coef(my.fit), add=TRUE, col="black") > curve (cbind (1, 0, x, 0*x) %*% coef(my.fit), add=TRUE, col="gray") > > dev.off() > } > )) > > > On Sun, Jun 22, 2008 at 6:26 PM, Donald Braman <[EMAIL PROTECTED]> > wrote: > > # I've tried to make this easy to paste into R, though it's probably > > so simple you won't need to. > > # I have some data (there are many more variables, but this is a > > reasonable approximation of it) > > > > # here's a fabricated data frame that is similar in form to mine: > > my.df <- data.frame(replicate(10, round(rnorm(100, mean=3.5, sd=1)))) > > var.list <- c("dv1", "dv2", "dv3", "iv1", "iv2", "iv3", "iv4", "iv5", > > "intv1", "intv2") > > names(my.df) <- var.list > > > > # I have some are DVs: > > dvs <- c("dv1", "dv2", "dv3") > > > > # some IVs: > > ivs <- c("iv1", "iv2", "iv3", "iv4", "iv5") > > > > # and some binary interaction variables: > > intvs <- c("intv1", "intv2") > > library(car) > > my.df[intvs] <- lapply(my.df[intvs], function(x) > > recode(x, recodes = "lo:3.5=0; 3.5:hi=1; ",as.factor.result = FALSE)) > > > > # now I loop through a series of interactions using the vector numbers: > > for(dv in 1:3) { > > for(iv in 4:8) { > > for (intv in 9:10) { > > jpeg(paste(names(my.df[iv]), names(my.df[dv]), names(my.df[intv]), > > ".jpg", sep="_")) > > with(data.frame(my.df), { > > my.fit <- lm( my.df[[dv]] ~ my.df[[iv]] + my.df[[intv]] + > > my.df[[iv]]:my.df[[intv]]) > > colors <- ifelse (my.df[[intv]] == 1, "black", "grey") > > plot(my.df[[iv]], my.df[[dv]], xlab=names(my.df[iv]), > > ylab=names(my.df[dv]), col=colors, pch=".") > > curve (cbind (1, 1, x, 1*x) %*% coef(my.fit), add=TRUE, col="black") > > curve (cbind (1, 0, x, 0*x) %*% coef(my.fit), add=TRUE, col="gray") > > }) > > dev.off() > > } > > } > > } > > > > > > # Question1: Works fine, but using the vector numbers feels kludgy -- > > especially if the variables in question aren't consecutive. > > # Is there a more elegant way of doing this with my lists of variable > > names? Something like this, for example: > > for(dv in dvs) { > > for(iv in ivs) { > > for (intv in intvs) { > > jpeg(paste(dv, iv, intv, ".jpg", sep="_")) > > with(data.frame(my.df), { > > my.fit <- lm(my.df[dv] ~ my.df[iv] + my.df[intv] + > my.df[iv]:my.df[intv]) > > colors <- ifelse (my.df[[intv]] == 1, "black", "grey") > > plot(my.df[iv], my.df[dv], xlab=iv, ylab=names(dv), col=colors, > pch=".") > > curve (cbind (1, 1, x, 1*x) %*% coef(my.fit), add=TRUE, col="black") > > curve (cbind (1, 0, x, 0*x) %*% coef(my.fit), add=TRUE, col="gray") > > }) > > dev.off() > > } > > } > > } > > > > # Clearly that's wrong -- why it's wrong is obscure to me, though! > > Please educate me! > > > > # Question2: Could this could be done by using "apply" rather than a > loop? > > # Or is looping better here bc there are several actions performed at > > each iteration? > > # I'm still trying to get my head around all the ways to ditch looping in > R. > > > > > > Donald Braman > > http://www.law.gwu.edu/Faculty/profile.aspx?id=10123 > > http://research.yale.edu/culturalcognition > > http://ssrn.com/author=286206 > > > > ______________________________________________ > > 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. > > > > > > -- > Jim Holtman > Cincinnati, OH > +1 513 646 9390 > > What is the problem you are trying to solve? > [[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.