using string as variable name in model

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

using string as variable name in model

Roni Kobrosly
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: using string as variable name in model

Phil Spector
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
  [hidden email]




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
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: using string as variable name in model

Thomas Lumley
In reply to this post by Roni Kobrosly


I think you're looking for the update() function.

    -thomas

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
>
> ______________________________________________
> [hidden email] 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.
>

Thomas Lumley Assoc. Professor, Biostatistics
[hidden email] University of Washington, Seattle

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: using string as variable name in model

Roni Kobrosly
update() works!
Thank you thomas


On Jun 4, 2010, at 11:47 AM, Thomas Lumley wrote:

>
>
> I think you're looking for the update() function.
>
>   -thomas
>
> 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
>>
>> ______________________________________________
>> [hidden email] 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.
>>
>
> Thomas Lumley Assoc. Professor, Biostatistics
> [hidden email] University of Washington, Seattle
>

______________________________________________
[hidden email] 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.