I'm currently working on regression and classification with R.
Therefore I'm using a formula similar to X ~ Y
in order to make predictions about X.
I am now trying to use a function inside a for-loop to make multiple predictions about different values on the X side of the tilde and constant values on the Y side. Something like this:
X1 ~ Y
X2 ~ Y
X3 ~ Y
with X1, X2, X3 and Y all being columns of data (data$X1 <- a, data$X2 <- b, data$X3 <- c, data$Y) if this is somehow important
so how can I dynamically select a variable inside the ~-Expression? I have tried something like this but it's not working:
# referring to "iris" data set with columns (Sepal.Length, Sepal.Width, Petal.Length, Petal.Width, Species)
getFormula <- function(variable){
variable ~ Sepal.Length + Sepal.Width + Species
}
petal.length.formula <- getFormula(Petal.Length)
petal.width.formula <- getFormula(Petal.Width)
i get this:
petal.length.formula: variable ~ Sepal.Lenght + Sepal.Width + Species
petal.width.formula: variable ~ Sepal.Lenght + Sepal.Width + Species
but i want to achieve this:
petal.length.formula: Petal.Length ~ Sepal.Lenght + Sepal.Width + Species
petal.width.formula: Petal.Width ~ Sepal.Lenght + Sepal.Width + Species
Since I have over 40 variables on the Y-Side and 10 variables on the X-Side, it would be really messy to type every single formula by hand. Can anybody help me with this issue?
I could not find a similar question since I have a hard time to figure out the keywords I have to use to find something about this.
If possible, I would prefer not to use any additional library since I'm rather new to R and want to first figure out the basics mechanics of R.
Since english is not my first language, I hope you can understand my question and I am of course happy to explain further if needed. Thank you in advance for your time!