[R] Specification of model formulae

Spencer Graves spencer.graves at pdf.com
Fri Apr 16 18:50:25 CEST 2004


      I often use the following extension of Christian's suggestion: 

 > nam <- paste("var", 1:5, sep="")
 > nam
[1] "var1" "var2" "var3" "var4" "var5"
 > nam. <- paste(nam, collapse="+")
 > nam.
[1] "var1+var2+var3+var4+var5"
 > nam2 <- paste("I(", nam, "^2)", sep="")
 > nam2
[1] "I(var1^2)" "I(var2^2)" "I(var3^2)" "I(var4^2)" "I(var5^2)"
 > nam2. <- paste(nam2, collapse="+")
 > nam2.
[1] "I(var1^2)+I(var2^2)+I(var3^2)+I(var4^2)+I(var5^2)"
 > mdl2 <- paste("y", "~ (", nam., ")^2 +", nam2.)
 > mdl2
[1] "y ~ ( var1+var2+var3+var4+var5 )^2 + 
I(var1^2)+I(var2^2)+I(var3^2)+I(var4^2)+I(var5^2)"
 > formula(mdl2)
y ~ (var1 + var2 + var3 + var4 + var5)^2 + I(var1^2) + I(var2^2) +
    I(var3^2) + I(var4^2) + I(var5^2)

      This will fit a full quadratic in the 5 variables. 

      If Uwe's solution won't work for you, this can be modified to work 
with any vector of appropriate variable names. 
      hope this helps. 
      spencer graves

kjetil at entelnet.bo wrote:

>On 16 Apr 2004 at 17:08, Christian Hennig wrote:
>
>You can build your formula along the ways of the following example, 
>without typing the 200 names:
>
>  
>
>>nam <- paste("var", 1:5, sep="", collapse="+")
>>nam
>>    
>>
>[1] "var1+var2+var3+var4+var5"
>  
>
>>nam <- paste("y", nam, sep="~")
>>nam
>>    
>>
>[1] "y~var1+var2+var3+var4+var5"
>  
>
>>as.formula(nam)
>>    
>>
>y ~ var1 + var2 + var3 + var4 + var5
>
>Kjetil Halvorsen
>
>  
>
>>Hi,
>>
>>is it true that the only way to tell rpart which variables to use is
>>to specify every single variable in the formula and separate them with
>>"+"?
>>
>>I wonder if this error...
>>
>>    
>>
>>>rotree <- rpart(powerdata[,1]~powerdata[,2:3],
>>>      
>>>
>>+                      method="class")
>>Error in model.frame(formula, rownames, variables, varnames, extras,
>>extranames,  : 
>> invalid variable type
>>
>>...can be prevented by other means than...
>>
>>    
>>
>>>rotree <- rpart(segments~V1.3+V1.9,
>>>      
>>>
>>+                      method="class",data=powerdata)
>>
>>This works.
>>(The variables here are powerdata[,1] and [,2:3].)
>>
>>But I do not want to write 200 variable names down, as this is the
>>real size of "powerdata".
>>
>>Is there any way around that?
>>
>>Christian
>>
>>
>>
>>**********************************************************************
>>* Christian Hennig Fachbereich Mathematik-SPST/ZMS, Universitaet
>>Hamburg hennig at math.uni-hamburg.de,
>>http://www.math.uni-hamburg.de/home/hennig/
>>######################################################################
>># ich empfehle www.boag-online.de
>>
>>______________________________________________
>>R-help at stat.math.ethz.ch mailing list
>>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide!
>>http://www.R-project.org/posting-guide.html
>>    
>>
>
>______________________________________________
>R-help at stat.math.ethz.ch mailing list
>https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>  
>




More information about the R-help mailing list