Define anonymour variables elsewhere in the code R

Define anonymour variables elsewhere in the code R

I am trying to render my code a bit less "unique" in the sense that I want this to be able to run regardless of what kind of dataset I use.

I'm essentially running different algorithms of classification and one of them is an ANN based on the neuralnet function, but the problem comes up with others as well:

model = neuralnet(
Class ~  var1 +var2 +var3,
data=train_data,
hidden=c(3,2,1),
linear.output = F)

how can I define the variables used in the model (i.e. what comes after the ~) using a vector or any other way that doesn't require me writing down the names of the variables? mostly because I want to be able, for example, to input something like variable from 4:78 or similar... and it gets pretty boring listing them all.


small edit: I know it's technically possible to use ~. and select all the variables, but it wpuld imply either create a secondary dataset with just the variables I want to use or remove the unwanted one from the initial dataset but i'd like to keep the number of datasets to a minimum and to make the code as light as possible since it already runs at a glacier pace

Answer

You can form a string then use as.formula():

lm(
  mpg ~ cyl + disp,
  data = mtcars
)
# Call:
# lm(formula = mpg ~ cyl + disp, data = mtcars)
# Coefficients:
# (Intercept)          cyl         disp  
#    34.66099     -1.58728     -0.02058  

as.formula(paste(names(mtcars[1]), "~", paste(names(mtcars)[2:3], collapse = " + ")))
# mpg ~ cyl + disp
# <environment: 0x1277810e8>
lm(
  as.formula(paste(names(mtcars[1]), "~", paste(names(mtcars)[2:3], collapse = " + "))),
  data = mtcars
)
# Call:
# lm(formula = as.formula(paste(names(mtcars[1]), "~", paste(names(mtcars)[2:3], 
#     collapse = " + "))), data = mtcars)
# Coefficients:
# (Intercept)          cyl         disp  
#    34.66099     -1.58728     -0.02058  

Enjoyed this article?

Check out more content on our blog or follow us on social media.

Browse more articles