Rita Carreira
2011-Jun-23 20:28 UTC
[R] Loops, Paste, Apply? What is the best way to set up a list of many equations?
Is there a way to apply paste to?list(form1 = EQ1, form2 = EQ2, form3 = EQ3,
form4 = EQ4)?such that I don't have to write form1=EQ1 for all my models?(I
might have a list of 20 or more)? I also need the EQs to read the formulas
associated with them.
For example, below, I was able to automate the name assignment but I could not
figure out how to?to set up the list using?paste or other functions:
V2system<-c(formula(paste("form1","=","EQ1")),
formula(paste("form2","=","EQ2")),?? ? ? ? ? ??? ?
? ? ? ? ? ? ? ? formula(paste("form3","=","EQ3")),
formula(paste("form4","=","EQ4")))names(V2system)<-paste("form",1:4,sep="")
Any ideas?Thank you so much in advance for any help provided.
Rita
######## HERE'S MY SAMPLE PROGRAM ########
YX<-as.data.frame(matrix(rnorm(280),ncol=14, nrow=20)) ?## generate
variablesnames(YX) <-c(paste("Y", 1:4, sep=""), ? ? ? ? ?
? ? ? ?## assign Y variables' names? ? ? ? ? ? ? paste("X", 1:10,
sep="")) ? ? ? ? ? ? ? ? ## assign X variables' names
EQ1 <- Y1 ~ X1 + X2 + X4 + X7 + X10 ? ? ?## equation 1 formulaEQ2 <- Y2 ~
X2 + X3 + X5 + X8 + X10 ? ? ?## equation 2 formulaEQ3 <- Y3 ~ X5 + X6 + X7 +
X9 ? ? ? ? ? ?## equation 3 formulaEQ4 <- Y4 ~ X1 + X3 + X4 + X6 + X9 ? ? ?
## equation 4 formula
## VERSION 1 FOR SETTING UP THE LIST:V1system <-list(form1 = EQ1, form2 =
EQ2, form3 = EQ3, form4 = EQ4)V1system
## VERSION 2 FOR SETTING UP THE
LIST:V2system<-c(formula(paste("form1","=","EQ1")),
formula(paste("form2","=","EQ2")),?? ? ? ? ? ?
formula(paste("form3","=","EQ3")),
formula(paste("form4","=","EQ4")))names(V2system)<-paste("form",1:4,sep="")V2system
######## HERE ARE MY RESULTS ########?
## RESULTS OF VERSIONS 1 AND 2 ARE EXACTLY THE SAME:
# $form1# Y1 ~ X1 + X2 + X4 + X7 + X10
# $form2# Y2 ~ X2 + X3 + X5 + X8 + X10
# $form3# Y3 ~ X5 + X6 + X7 + X9
# $form4# Y4 ~ X1 + X3 + X4 + X6 + X9
## STRUCTURE OF THE LISTS IS SAME FOR BOTH VERSIONS AS WELL:
# structure(list(form1 = quote(Y1 ~ X1 + X2 + X4 + X7 + X10),?# ? ? ? ? ? ? ?
form2 = quote(Y2 ~ X2 + X3 + X5 + X8 + X10),?# ? ? ? ? ? ? ? form3 = quote(Y3 ~
X5 + X6 + X7 + X9),?# ? ? ? ? ? ? ? form4 = quote(Y4 ~ X1 + X3 + X4 + X6 +
X9)),?# ? ? ? ? ?.Names = c("form1", "form2",
"form3", "form4"))
Rita
===================================="If you think education is expensive,
try ignorance."--Derek Bok
