Hi,
I am having difficulty entering a 'programmable' argument into the
multinom function from the nnet package. Interactively, I can get the function
to work fine by calling it this way:
z1=multinom(formula = class.ind(grp[-outgroup])~ (PC1 + PC2 + PC3),
data=data.frame(scores))
However I need to be able to change the number of variables I am looking for in
'scores' and so am trying to call it this way...
z1=multinom(formula = class.ind(grp[-outgroup])~ eval(parse(text=PCnames)),
data=data.frame(scores))
...where, for example, PCnames = c("PC1", "+",
"PC2", "+", "PC3")
This gives no error messages, but only the last variable (in this case PC3) gets
considered in the model. z1 looks like this:
(Intercept) eval(parse(text = PCnames))
2 3.530352 -116.87140
3 -1.308613 13.59134
4 3.662172 -57.52198
5 -1.216041 -242.38827
6 -9.377894 -367.71614
7 -3.145738 -286.19766
Rather than this:
(Intercept) PC1 PC2 PC3
2 288.97131 889.281 3776.5837 -2105.751
3 -712.53519 2775.663 8490.5724 8602.834
4 229.17772 4234.950 329.6995 -2182.238
5 85.54585 -3036.657 3968.2517 -3450.070
6 -676.55377 -9545.785 2422.5340 -7183.686
7 -631.91921 10997.432 -3310.2905 -5348.513
Any help would be much appreciated.
Thanks,
Daniel Crouch
Daniel Crouch
Research Student
Department of Medical & Molecular Genetics
King's College London
8th Floor, Tower Wing
Guy's Hospital
London SE1 9RT
United Kingdom
Forget eval(parse(text = )) See ?as.formula ?update.formula and try out the example() s there. HTH, Chuck On Mon, 16 Feb 2009, Crouch, Daniel wrote:> Hi, > > I am having difficulty entering a 'programmable' argument into the multinom function from the nnet package. Interactively, I can get the function to work fine by calling it this way: > > z1=multinom(formula = class.ind(grp[-outgroup])~ (PC1 + PC2 + PC3), data=data.frame(scores)) > > However I need to be able to change the number of variables I am looking for in 'scores' and so am trying to call it this way... > > z1=multinom(formula = class.ind(grp[-outgroup])~ eval(parse(text=PCnames)), data=data.frame(scores)) > > ...where, for example, PCnames = c("PC1", "+", "PC2", "+", "PC3") > > This gives no error messages, but only the last variable (in this case PC3) gets considered in the model. z1 looks like this: > > (Intercept) eval(parse(text = PCnames)) > 2 3.530352 -116.87140 > 3 -1.308613 13.59134 > 4 3.662172 -57.52198 > 5 -1.216041 -242.38827 > 6 -9.377894 -367.71614 > 7 -3.145738 -286.19766 > > Rather than this: > > (Intercept) PC1 PC2 PC3 > 2 288.97131 889.281 3776.5837 -2105.751 > 3 -712.53519 2775.663 8490.5724 8602.834 > 4 229.17772 4234.950 329.6995 -2182.238 > 5 85.54585 -3036.657 3968.2517 -3450.070 > 6 -676.55377 -9545.785 2422.5340 -7183.686 > 7 -631.91921 10997.432 -3310.2905 -5348.513 > > Any help would be much appreciated. > > Thanks, > Daniel Crouch > > > Daniel Crouch > Research Student > Department of Medical & Molecular Genetics > King's College London > 8th Floor, Tower Wing > Guy's Hospital > London SE1 9RT > United Kingdom > ______________________________________________ > 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. >Charles C. Berry (858) 534-2098 Dept of Family/Preventive Medicine E mailto:cberry at tajo.ucsd.edu UC San Diego http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Thanks for your help,
I managed to get it working like this:
fml<-as.formula(paste("y ~", paste(PCnames,
collapse="+")))
y = class.ind(grp[outgroup])
z1=multinom(formula = fml, data=data.frame(scores))
Daniel Crouch
**********QUOTE:********
Forget eval(parse(text = ))
See
?as.formula
?update.formula
and try out the example() s there.
HTH,
Chuck
On Mon, 16 Feb 2009, Crouch, Daniel wrote:
> > Hi,
> >
> > I am having difficulty entering a 'programmable' argument into
the multinom function from the nnet package. Interactively, I can get the
function to work fine by calling it this way:
> >
> > z1=multinom(formula = class.ind(grp[-outgroup])~ (PC1 + PC2 + PC3),
data=data.frame(scores))
> >
> > However I need to be able to change the number of variables I am
looking for in 'scores' and so am trying to call it this way...
> >
> > z1=multinom(formula = class.ind(grp[-outgroup])~
eval(parse(text=PCnames)), data=data.frame(scores))
> >
> > ...where, for example, PCnames = c("PC1", "+",
"PC2", "+", "PC3")
> >
> > This gives no error messages, but only the last variable (in this case
PC3) gets considered in the model. z1 looks like this:
> >
> > (Intercept) eval(parse(text = PCnames))
> > 2 3.530352 -116.87140
> > 3 -1.308613 13.59134
> > 4 3.662172 -57.52198
> > 5 -1.216041 -242.38827
> > 6 -9.377894 -367.71614
> > 7 -3.145738 -286.19766
> >
> > Rather than this:
> >
> > (Intercept) PC1 PC2 PC3
> > 2 288.97131 889.281 3776.5837 -2105.751
> > 3 -712.53519 2775.663 8490.5724 8602.834
> > 4 229.17772 4234.950 329.6995 -2182.238
> > 5 85.54585 -3036.657 3968.2517 -3450.070
> > 6 -676.55377 -9545.785 2422.5340 -7183.686
> > 7 -631.91921 10997.432 -3310.2905 -5348.513
> >
> > Any help would be much appreciated.
> >
> > Thanks,
> > Daniel Crouch
> >
> >
> > Daniel Crouch
> > Research Student
> > Department of Medical & Molecular Genetics
> > King's College London
> > 8th Floor, Tower Wing
> > Guy's Hospital
> > London SE1 9RT
> > United Kingdom
> > ______________________________________________
> > 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.
> >
Charles C. Berry (858) 534-2098
Dept of Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/ La Jolla, San Diego 92093-0901
Daniel Crouch
Research Student
Department of Medical & Molecular Genetics
King's College London
8th Floor, Tower Wing
Guy's Hospital
London SE1 9RT
United Kingdom