[R] using string as variable name in model

From: Roni Kobrosly <slyron84_at_gmail.com>
Date: Thu, 03 Jun 2010 20:02:25 -0400


Hi,

I made a small table of strings that will serve as variable names for lm models I will run. The table looks like this:

> varnames

   num                        name
1    1      zCANTAB_log_IED_totaltrials
2    2       zCANTAB_log_IED_preED
3    3        zCANTAB_logPALerrors
4    4           zCANTAB_PALstages
5    5              zCANTAB_logRTI
6    6       zCANTAB_RVP_Totalmisses
7    7      zCANTAB_log_RVP_falsealarm
8    8           zCANTAB_DMS_12000
9    9         zCANTAB_PRM_Percent
10  10         zCANTAB_SRM_Percent
11  11     zCANTAB_sqrt_SWM_within
12  12      zCANTAB_sqrt_SWM_Total
13  13                  zSS_WJ_PC 
14  14                 zSS_WJ_CALC
15  15                   zSS_WJ_LW
16  16                   zSS_WJ_AP
17  17                   zSS_WJ_MF


What I'm trying to do is

varnames[1,2] -> outcome                         

lm(outcome ~ income + covariate1 + coviarate2, data=my.data) -> model

I get the following error message:
"Error in model.frame.default(formula = outcome ~ Hollings_Enroll + Child_sex + :   variable lengths differ (found for 'Hollings_Enroll')"

But when I run the model:
lm(zCANTAB_log_IED_totaltrials ~ income + covariate1 + coviarate2, data=my.data) -> model

it works fine. Eventually, once I resolve this, I'd like to make a custom R function to automatically generate 17 regression models with these 17 outcome variable strings. Do you have any ideas how to resolve this error?

Thanks!
-Roni



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. Received on Fri 04 Jun 2010 - 00:09:20 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Fri 04 Jun 2010 - 16:00:27 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive