Jason Ching Yuk Jason Ching Yuk - 2 months ago 12
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)
return(fit)
}

fitRegression(x2)


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:

fitRegression(x2)


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

and if:

fitRegression(x3)


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

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
  dependentVariableIndex<-which(colnames(mydata)==dependentVariable)
  independentVariableIndices<-which(colnames(mydata)!=dependentVariable)

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

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

# reconcile results
fitRegression(sampleData, "A")
Comments