Hi,
I have a list of formulae that I need to modify. For each formula, I need
to add an interaction term between the first independent variable and the
last one. I want to write a function to achieve that because the list of
formulae to modify is long.

For example, if the formula is y ~ x1 + x2 + x3 + x4, then I need to turn
it either into

y ~ x1*x4 + x2 + x3

or

y ~ x1 + x2 + x3 + x4 + x4:x1

(I know they are the same, but one may be easier to work with than the
other).


Suppose I have the formula a which is defined as a <- formula(y ~
x1+x2+x3+x4)

I know how to access to the first and last independent variables of my
formula:

firstvar <- all.vars(a[[3]])[1]
lastvar <- all.vars(a[[3]])[length( all.vars(a[[3]]))]


What I can't figure out is how to use update.formula() to include my
interaction term in order to get y ~ x1+x2+x3+x4 + x1:x4. Specifically,

update.formula(a, ~ . + paste(all.vars(a[[3]])[1],firstvar, lastvar, sep =
':'))

is not producing y ~ x1+x2+x3+x4 + x1:x4.

Any idea?

Thanks in advance,
MP

        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to