I want to do liner regression analysis between response variable(y) and predictor variables(for each independently considering variable pindex as confounding variable) and eventually I want to plot the significant predictor variable estimated regression vs the response variable in a ggplot2.
I know the formula how to do liner regression and I can do it for each one by one( lm(y ~ pindex + predictor, data = surgical)).
For example, for liver and enzyme
# for enzyme
modle_enzyme <- lm(y ~ pindex + enzyme_test)
enzy <- pred <- broom::tidy(modle_enzyme )
# for liver
modle_liver <- lm(y ~ pindex + liver_test) etc for each
liver <- broom::tidy(modle_liver )
all <- bind_rows(enzy, liver, ...)
But doing this is really tiresome for large data set.
So I was wondering if some one can show me how to do it using loop function.
The data can be found here
Here is how I might go about it. I would pivot the data to long format, nest each predictor into its own data set (along with y and pindex), and then you can easily fit all of your models, tidy them with broom, and then unnest them to have all the coefficients together in one data set.
@mattwarkentin solution is really nice and concise. I would follow his way. But, if you like to do a bit more primitive way using for loop, you can try as follows.