[R] identifying a column name correctly to use in a formula

Rui Barradas rui1174 at sapo.pt
Thu Mar 1 04:24:02 CET 2012


Hello,

>
> I have a large matrix (SNPs) that I want to cycle over with logistic
> regression with interaction terms. I have made a loop but I am struggling
> to identify to the formula the name of the column in a way which is
> meaningful to the formula. It errors becasue it is not evaluated proporly. 
> You have must first write the formula in full, using 'paste'.
> 

Try

DF <- data.frame(Resp=rnorm(10), B=rnorm(10), C=rnorm(10),
Interaction=rnorm(10))
#DF

for(i in 2:3){
	cname <- colnames(DF)[i]
	#
	# In 3 steps to be more readable
	Regr <- paste(cname, "Interaction", sep="*")
	fmlaText <- paste("Resp", Regr, sep="~")
	# After step 2 it's already printable
	print(fmlaText)
	# Step 3: transform it into a formula object
	fmla <- as.formula(fmlaText)
	model1 <- glm(fmla, data=DF)
	print(summary(model1))
}

You have must first write the formula in full, using 'paste'.

Hope this helps,

Rui Barradas


--
View this message in context: http://r.789695.n4.nabble.com/identifying-a-column-name-correctly-to-use-in-a-formula-tp4433605p4433924.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list