0

suppose i have such dataset.

mydata=structure(list(x1 = 1:3, x2 = 1:3, x3 = 1:3, x4 = 1:3, x5 = 1:3, 
    x6 = 1:3), class = "data.frame", row.names = c(NA, -3L))

how to perform a regression by swapping the dependent and independent variables? For example we can create models like

mymodel1=lm(x1~x2,data=mydata)
оr
x3~x5
or
x5~x1

and so on also i can combinate predcitors.For example

mymod=lm(x1~x2+x6+x5)
or
mymod2=lm(x3~x2+x4+x5)

and so on

is there way to perform multiply regressions models in R via combining variables (dependent-independent(s)?

r2evans
  • 77,184
  • 4
  • 55
  • 96
cbool
  • 197
  • 1
  • 8
  • In your case you will have to dynamically create the formula, using strings. it is a question that has been answered quite a few times on SO. – Oliver Feb 16 '21 at 15:15

1 Answers1

1

@cbool

Untill today I didn't take any dataset with more than one dependent variable. Always you have the dependent (target)variable what you want do predict and independent variables, what you will use to predict.

For you create different models, you can use some of codes bellow:

Model with all independent variable:

model1 <- lm(x1 ~ . , data = mydata) # Using "." you will have a model with  all independent variables.

Model with all independent variable less one:

model2 <- lm(x1 ~ . -x2 , data = mydata) # Using "-" you say to model that you want all variables less that one. You can remove how many variables you want

Second example:

model3 <- lm(x1 ~ . -x2 -x3 -x4, data = mydata) # so on and so forth

Model to choose the best independent variables:

You can use randomForest algorithm to determine what are the best independent variables to use.

model4 <- randomForest(x1 ~ . , 
                       data = bikes, 
                       ntree = 100, 
                       nodesize = 10,
                       importance = TRUE)

Remember, its necessary you use importance=TRUE parameter to random forest determine the variable, if not, they will create a train model.

Hope was helpful.