[R] using string as variable name in model

Phil Spector spector at stat.berkeley.edu
Fri Jun 4 02:58:18 CEST 2010


Roni -
    Try this:
lm(formula(paste(outcome,'income + covariate1 + coviarate2',sep='~')), data=my.data) -> model


 					- Phil Spector
 					 Statistical Computing Facility
 					 Department of Statistics
 					 UC Berkeley
 					 spector at stat.berkeley.edu




On Thu, 3 Jun 2010, Roni Kobrosly wrote:

> 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.
>



More information about the R-help mailing list