Moumita Das
2009-Jul-22 13:50 UTC
[R] How to dynamically generate lm() function arguments?
Hi All, How do you dynamically generate the arguments for the lm() function when your items vary for each database. Say in my case for a particular database i have items from i1 to i15 . In the code below there a line like this :-- item_cat_fit<-lm(as.numeric(item_item_table$i1) ~ as.numeric(item_item_table$i2) + as.numeric(item_category_table$i3) ) *# this gives proper results,i am accessing each column of the dataset using column names. * how do i dynamically generate this :--- item_cat_fit<-lm(as.numeric(item_item_table[item_item_dependent_var_counter]) ~ as.numeric(item_item_table[item_item_independent_var_counter ] ) ) *Because of this as.numeric(item_item_table[item_item_dependent_var_counter]) i get error that you cannot convert a list to double.I tried doing this as.numeric(as.matrix(item_item_table[item_item_dependent_var_counter]))* I also tried doing this mode(item_item_table[item_item_dependent_var_counter])<-"numeric" but of no use. for(item_item_dependent_var_counter in 1:item_item_size[1]) # no of all items { for(item_item_independent_var_counter in 1:(item_item_size[1] - 1)) { item_item_third_var_data<-contrld_third_var(stringOfItemItemToDataFrameOfItemItem[item_item_dependent_var_counter,1],x<-stringOfItemItemToDataFrameOfItemItem,item__table,regr_type<-"ii") item_cat_fit<-lm(as.numeric(item_item_table$i1) ~ as.numeric(item_item_table$i2) ) item_cat_fit<-lm(as.numeric(item_item_table[item_item_dependent_var_counter]) ~ as.numeric(item_item_table[item_item_independent_var_counter ] ) ) #print(summary(item_item_fit)) } } How can the lm() arguments be generated dynamically.?Would be grateful if anyone can come up with a solution for my problem. -- Thanks Moumita [[alternative HTML version deleted]]
Gabor Grothendieck
2009-Jul-22 14:50 UTC
[R] How to dynamically generate lm() function arguments?
Using the built in anscombe data frame we can regress y1 on x2 and x4 like this (place the response first): lm(anscombe[c("y1", "x2", "x4")]) On Wed, Jul 22, 2009 at 9:50 AM, Moumita Das<das.moumita.online at gmail.com> wrote:> Hi All, > How do you dynamically generate the arguments for the lm() function when > your items vary for each database. > Say in my case for a particular database i have items from i1 to i15 . > > In the code below there a line like this :-- > ?item_cat_fit<-lm(as.numeric(item_item_table$i1) ~ > as.numeric(item_item_table$i2) + as.numeric(item_category_table$i3) ?) *# > this gives proper results,i am accessing each column of the dataset using > column names. > * > how do i dynamically generate this :--- > item_cat_fit<-lm(as.numeric(item_item_table[item_item_dependent_var_counter]) > ~ ?as.numeric(item_item_table[item_item_independent_var_counter ] ) ) > > *Because of this as.numeric(item_item_table[item_item_dependent_var_counter]) > i get error that you cannot convert a list to double.I tried doing this > as.numeric(as.matrix(item_item_table[item_item_dependent_var_counter]))* > > I also tried doing this > mode(item_item_table[item_item_dependent_var_counter])<-"numeric" ?but of no > use. > > for(item_item_dependent_var_counter in 1:item_item_size[1]) ?# no of all > items > ?{ > > > ? ? ?for(item_item_independent_var_counter in 1:(item_item_size[1] - 1)) > ? ? ?{ > > > item_item_third_var_data<-contrld_third_var(stringOfItemItemToDataFrameOfItemItem[item_item_dependent_var_counter,1],x<-stringOfItemItemToDataFrameOfItemItem,item__table,regr_type<-"ii") > > > ?item_cat_fit<-lm(as.numeric(item_item_table$i1) ~ > as.numeric(item_item_table$i2) ) > ?item_cat_fit<-lm(as.numeric(item_item_table[item_item_dependent_var_counter]) > ~ ?as.numeric(item_item_table[item_item_independent_var_counter ] ) ) > > ? ? ? ? ? ? ? ? ? ? ? ? ? ?#print(summary(item_item_fit)) > ? ? ? } > } > > How can the lm() arguments be generated dynamically.?Would be grateful if > anyone can come up with a solution for my problem. > -- > Thanks > Moumita > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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. >