Jason Ching Yuk Jason Ching Yuk - 1 year ago 40
R Question

Linear Regression Function

In my dataset I have 6 variables(x1,x2,x3,x4,x5,x6), i wish to create a function that allows me to input one variable and it will do the formula with the rest of the variables in the data set.

For instance,

fitRegression <- function(data, dependentVariable) {
fit = lm(formula = x1 ~., data = data1)


However, this function only returns me with results of x1. My desire result will be inputting whatever variables and will automatically do the formula with the rest of the variables.

For Example:


should subtract x2 from the variable list therefore we only compare x2 with x1,x3,x4,x5,x6.

and if:


should subtract x3 from the comparable list, therefore we compare x3 with x1,x2,x4,x5,x6.

Is there any ways to express this into my function, or even a better function.

Answer Source

You can do it like this:

# sample data
sampleData <- data.frame(matrix(rnorm(500),100,5))
colnames(sampleData) <- c("A","B","C","D","E")

# function
fitRegression <- function(mydata, dependentVariable) {

  # select your independent and dependent variables

  fit = lm(formula = as.formula(paste(colnames(mydata)[dependentVariableIndex], "~", paste(colnames(mydata)[independentVariableIndices], collapse = "+"), sep = "" )), data = mydata) 

# ground truth
lm(formula = A~B+C+D+E, data = sampleData)

# reconcile results
fitRegression(sampleData, "A")