Home > database >  R How to pass a list of variables names together into a function
R How to pass a list of variables names together into a function

Time:12-06

I am trying to build a linear regression with hundreds of interaction terms. I am now manually typing them into the equation.

model <- lm(Y ~ A   B   C   A*X1   A*X2   A*X3 ... A*X100   B*X1   B*X2   B*X3... B*X100, data)

is there a way to code this more efficiently? I am thinking about something like:

Group <- X1   X2   X3  ...   X99   X100
model <- lm(Y~ A   B   C   A*Group   B*Group)

Is this possible to be achieved in R with any packages or functions?

CodePudding user response:

You could try:

xs <- paste(paste("X", 1:100, sep=""),collapse=" ")
form1 <- as.formula(sprintf("Y~ C (A B)*(%s)",xs))
model <- lm(form1)
form1

Y ~ C   (A   B) * (X1   X2   X3   X4   X5   X6   X7   X8   X9   
    X10   X11   X12   X13   X14   X15   X16   X17   X18   X19   
    X20   X21   X22   X23   X24   X25   X26   X27   X28   X29   
    X30   X31   X32   X33   X34   X35   X36   X37   X38   X39   
    X40   X41   X42   X43   X44   X45   X46   X47   X48   X49   
    X50   X51   X52   X53   X54   X55   X56   X57   X58   X59   
    X60   X61   X62   X63   X64   X65   X66   X67   X68   X69   
    X70   X71   X72   X73   X74   X75   X76   X77   X78   X79   
    X80   X81   X82   X83   X84   X85   X86   X87   X88   X89   
    X90   X91   X92   X93   X94   X95   X96   X97   X98   X99   
    X100)

The first line builds the X1 X2 ... X100 part. The second line creates the formula with A, B and C main effects with A and B crossed with each X. as.formula will turn a string into a formula.

CodePudding user response:

Consider reformulate by building a vector of right-hand side variables and then pass it as first argument:

rhs_vars <- c(
    "A", "B", "C", 
    paste0("A*X", 1:100),
    paste0("B*X", 1:100)
)
               
model <- lm(reformulate(rhs_vars, "Y"), data)

CodePudding user response:

We can use a combination of paste0 and reformulate:

Group <- paste0("X", 1:100)
rhs_expanded<-c(paste0("A*", Group), paste0("B*", Group))
lm_formula<-reformulate(response = 'Y', termlabels = c('A', 'B', 'C', rhs_expanded))

lm(formula = lm_formula, data)

See the lm_formula object that is produced by reformulate:

lm_formula

Y ~ A   B   C   A * X1   A * X2   A * X3   A * X4   A * X5   
    A * X6   A * X7   A * X8   A * X9   A * X10   A * X11   A * 
    X12   A * X13   A * X14   A * X15   A * X16   A * X17   A * 
    X18   A * X19   A * X20   A * X21   A * X22   A * X23   A * 
    X24   A * X25   A * X26   A * X27   A * X28   A * X29   A * 
    X30   A * X31   A * X32   A * X33   A * X34   A * X35   A * 
    X36   A * X37   A * X38   A * X39   A * X40   A * X41   A * 
    X42   A * X43   A * X44   A * X45   A * X46   A * X47   A * 
    X48   A * X49   A * X50   A * X51   A * X52   A * X53   A * 
    X54   A * X55   A * X56   A * X57   A * X58   A * X59   A * 
    X60   A * X61   A * X62   A * X63   A * X64   A * X65   A * 
    X66   A * X67   A * X68   A * X69   A * X70   A * X71   A * 
    X72   A * X73   A * X74   A * X75   A * X76   A * X77   A * 
    X78   A * X79   A * X80   A * X81   A * X82   A * X83   A * 
    X84   A * X85   A * X86   A * X87   A * X88   A * X89   A * 
    X90   A * X91   A * X92   A * X93   A * X94   A * X95   A * 
    X96   A * X97   A * X98   A * X99   A * X100   B * X1   B * 
    X2   B * X3   B * X4   B * X5   B * X6   B * X7   B * X8   
    B * X9   B * X10   B * X11   B * X12   B * X13   B * X14   
    B * X15   B * X16   B * X17   B * X18   B * X19   B * X20   
    B * X21   B * X22   B * X23   B * X24   B * X25   B * X26   
    B * X27   B * X28   B * X29   B * X30   B * X31   B * X32   
    B * X33   B * X34   B * X35   B * X36   B * X37   B * X38   
    B * X39   B * X40   B * X41   B * X42   B * X43   B * X44   
    B * X45   B * X46   B * X47   B * X48   B * X49   B * X50   
    B * X51   B * X52   B * X53   B * X54   B * X55   B * X56   
    B * X57   B * X58   B * X59   B * X60   B * X61   B * X62   
    B * X63   B * X64   B * X65   B * X66   B * X67   B * X68   
    B * X69   B * X70   B * X71   B * X72   B * X73   B * X74   
    B * X75   B * X76   B * X77   B * X78   B * X79   B * X80   
    B * X81   B * X82   B * X83   B * X84   B * X85   B * X86   
    B * X87   B * X88   B * X89   B * X90   B * X91   B * X92   
    B * X93   B * X94   B * X95   B * X96   B * X97   B * X98   
    B * X99   B * X100
  • Related