Paul Bernal
2023-Jul-17 17:24 UTC
[R] Obtaining R-squared from All Possible Combinations of Linear Models Fitted
Dear friends, I need to automatically fit all possible linear regression models (with all possible combinations of regressors), and found the MuMIn package, which has the dredge function. This is the dataset I am working with:> dput(final_frame)structure(list(y = c(41.9, 44.5, 43.9, 30.9, 27.9, 38.9, 30.9, 28.9, 25.9, 31, 29.5, 35.9, 37.5, 37.9), x1 = c(6.6969, 8.7951, 9.0384, 5.9592, 4.5429, 8.3607, 5.898, 5.6039, 4.9176, 6.2712, 5.0208, 5.8282, 5.9894, 7.5422), x4 = c(1.488, 1.82, 1.5, 1.121, 1.175, 1.777, 1.24, 1.501, 0.998, 0.975, 1.5, 1.225, 1.256, 1.69 ), x8 = c(22, 50, 23, 32, 40, 48, 51, 32, 42, 30, 62, 32, 40, 22), x2 = c(1.5, 1.5, 1, 1, 1, 1.5, 1, 1, 1, 1, 1, 1, 1, 1.5), x7 = c(3, 4, 3, 3, 3, 4, 3, 3, 4, 2, 4, 3, 3, 3)), class "data.frame", row.names = c(NA, -14L)) I started with the all regressor model, which I called globalmodel as follows: #Fitting Regression model with all possible combinations of regressors options(na.action = "na.fail") # change the default "na.omit" to prevent models globalmodel <- lm(y~., data=final_frame) Then, the following code provides the different coefficients (for regressors and the intercept) for each of the possible model combinations: combinations <- dredge(globalmodel) print(combinations) I would like to retrieve the R-squared generated by each combination, but have not been able to get it thus far. Any guidance on how to retrieve the R-squared from all linear model combinations would be greatly appreciated. Kind regards, Paul [[alternative HTML version deleted]]
John C Frain
2023-Jul-17 19:25 UTC
[R] Obtaining R-squared from All Possible Combinations of Linear Models Fitted
MuMln is a package designed to select optimum models mainly based on information criteria. R-squared is not a suitable criterion for this purpose. As far as I can see is not covered in this package. (I presume you already know that R-squared for the model with all possible regressors is at least as great as R with any subset of the regressors). If you want to calculate all these R-squared's it should be easy to write a small routine to estimate them. I am very curious as to why you wish to do this. John C Frain. 3 Aranleigh Park Rathfarnham Dublin 14 Ireland www.tcd.ie/Economics/staff/frainj/home.html https://jcfrain.wordpress.com/ https://jcfraincv19.wordpress.com/ mailto:frainj at tcd.ie mailto:frainj at gmail.com On Mon, 17 Jul 2023 at 18:25, Paul Bernal <paulbernal07 at gmail.com> wrote:> Dear friends, > > I need to automatically fit all possible linear regression models (with all > possible combinations of regressors), and found the MuMIn package, which > has the dredge function. > > This is the dataset I am working with: > > dput(final_frame) > structure(list(y = c(41.9, 44.5, 43.9, 30.9, 27.9, 38.9, 30.9, > 28.9, 25.9, 31, 29.5, 35.9, 37.5, 37.9), x1 = c(6.6969, 8.7951, > 9.0384, 5.9592, 4.5429, 8.3607, 5.898, 5.6039, 4.9176, 6.2712, > 5.0208, 5.8282, 5.9894, 7.5422), x4 = c(1.488, 1.82, 1.5, 1.121, > 1.175, 1.777, 1.24, 1.501, 0.998, 0.975, 1.5, 1.225, 1.256, 1.69 > ), x8 = c(22, 50, 23, 32, 40, 48, 51, 32, 42, 30, 62, 32, 40, > 22), x2 = c(1.5, 1.5, 1, 1, 1, 1.5, 1, 1, 1, 1, 1, 1, 1, 1.5), > x7 = c(3, 4, 3, 3, 3, 4, 3, 3, 4, 2, 4, 3, 3, 3)), class > "data.frame", row.names = c(NA, > -14L)) > > I started with the all regressor model, which I called globalmodel as > follows: > #Fitting Regression model with all possible combinations of regressors > options(na.action = "na.fail") # change the default "na.omit" to prevent > models > globalmodel <- lm(y~., data=final_frame) > > Then, the following code provides the different coefficients (for > regressors and the intercept) for each of the possible model combinations: > combinations <- dredge(globalmodel) > print(combinations) > I would like to retrieve the R-squared generated by each combination, but > have not been able to get it thus far. > > Any guidance on how to retrieve the R-squared from all linear model > combinations would be greatly appreciated. > > Kind regards, > Paul > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >[[alternative HTML version deleted]]
Jan van der Laan
2023-Jul-18 11:49 UTC
[R] Obtaining R-squared from All Possible Combinations of Linear Models Fitted
The dredge function has a `extra` argument to get other statistics: optional additional statistics to be included in the result, provided as functions, function names or a list of such (preferably named or quoted). As with the rank argument, each function must accept as an argument a fitted model object and return (a value coercible to) a numeric vector. This could be, for instance, additional information criteria or goodness-of-fit statistics. The character strings "R^2" and "adjR^2" are treated in a special way and add a likelihood-ratio based R? and modified-R? to the result, respectively (this is more efficient than using r.squaredLR directly). HTH Jan On 17-07-2023 19:24, Paul Bernal wrote:> Dear friends, > > I need to automatically fit all possible linear regression models (with all > possible combinations of regressors), and found the MuMIn package, which > has the dredge function. > > This is the dataset I am working with: >> dput(final_frame) > structure(list(y = c(41.9, 44.5, 43.9, 30.9, 27.9, 38.9, 30.9, > 28.9, 25.9, 31, 29.5, 35.9, 37.5, 37.9), x1 = c(6.6969, 8.7951, > 9.0384, 5.9592, 4.5429, 8.3607, 5.898, 5.6039, 4.9176, 6.2712, > 5.0208, 5.8282, 5.9894, 7.5422), x4 = c(1.488, 1.82, 1.5, 1.121, > 1.175, 1.777, 1.24, 1.501, 0.998, 0.975, 1.5, 1.225, 1.256, 1.69 > ), x8 = c(22, 50, 23, 32, 40, 48, 51, 32, 42, 30, 62, 32, 40, > 22), x2 = c(1.5, 1.5, 1, 1, 1, 1.5, 1, 1, 1, 1, 1, 1, 1, 1.5), > x7 = c(3, 4, 3, 3, 3, 4, 3, 3, 4, 2, 4, 3, 3, 3)), class > "data.frame", row.names = c(NA, > -14L)) > > I started with the all regressor model, which I called globalmodel as > follows: > #Fitting Regression model with all possible combinations of regressors > options(na.action = "na.fail") # change the default "na.omit" to prevent > models > globalmodel <- lm(y~., data=final_frame) > > Then, the following code provides the different coefficients (for > regressors and the intercept) for each of the possible model combinations: > combinations <- dredge(globalmodel) > print(combinations) > I would like to retrieve the R-squared generated by each combination, but > have not been able to get it thus far. > > Any guidance on how to retrieve the R-squared from all linear model > combinations would be greatly appreciated. > > Kind regards, > Paul > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.