I'm starting to use Rstudio, but I'm having trouble understanding how does the combinat :: combn () function works. (I can't really understand how to use it in the following academic exercise:

Please Use the dataset: "mtcars" and answer: 1- Build an OLS model, for the fuel efficiency variable as a result, this time we limit the number of predictors. 2- Of the 10 predictors, let's limit the number of predictors from 1 to 5 as a maximum. 3- Create a routine that repeats all the possible permutations and deduce which "TOP-3" models that could have the best performance in terms of effect size and importance.

I guess the steps to follow are:

1- First I have to get all the permutations and evaluate the models using a suitable KPI (I think it would be necessary to pack the whole function in a loop because of the documentation I have read so far using the function combinat :: combn ( )). -> The problem is that I already searched the internet without success for an example that can give me insight into how to start building the solution, I explored the data in many ways, and end the data using the "lm" function but I do not know how to use it with the function "combn" ... or perhaps it is not the best option or the most appropriate. 2- And then choose the best KPI.

Below I left what I have built to explore the data with my poor knowledge of Rstudio and what I have managed to do with different internet sources:

version # --- I leave the version that you are using because I don't know if it affects later # platform x86_64-w64-mingw32 # arch x86_64 # os mingw32 # system x86_64, mingw32 # status # major 4 # minor 0.4 # year 2021 # month 02 # day 15 # svn rev 80002 # language R # version.string **R version 4.0.4 (2021-02-15)** # nickname Lost Library Book. library(tidyverse) # 25-03-2021 # -- Attaching packages ------------------------------------------------------------------------------ tidyverse 1.3.0 -- # v ggplot2 3.3.3 v purrr 0.3.4 # v tibble 3.1.0 v dplyr 1.0.5 # v tidyr 1.1.3 v stringr 1.4.0 # v readr 1.4.0 v forcats 0.5.1 # -- Conflicts --------------------------------------------------------------------------------- tidyverse_conflicts() -- # x dplyr::filter() masks stats::filter() # x dplyr::lag() masks stats::lag() data("mtcars") view(mtcars) ?mtcars #A data frame with 32 observations on 11 (numeric) variables. #[, 1] mpg Miles/(US) gallon --------->>>>>>> (fuel consumption efficiency) <<<<<<<------------------- #[, 2] cyl Number of cylinders #[, 3] disp Displacement (cu.in.) #[, 4] hp Gross horsepower #[, 5] drat Rear axle ratio #[, 6] wt Weight (1000 lbs) #[, 7] qsec 1/4 mile time #[, 8] vs Engine (0 = V-shaped, 1 = straight) #[, 9] am Transmission (0 = automatic, 1 = manual) #[,10] gear Number of forward gears #[,11] carb Number of carburetors summary(mtcars) # I explore the data a bit: #mpg cyl disp hp drat wt qsec #Min. :10.40 Min. :4.000 Min. : 71.1 Min. : 52.0 Min. :2.760 Min. :1.513 Min. :14.50 #1st Qu.:15.43 1st Qu.:4.000 1st Qu.:120.8 1st Qu.: 96.5 1st Qu.:3.080 1st Qu.:2.581 1st Qu.:16.89 #Median :19.20 Median :6.000 Median :196.3 Median :123.0 Median :3.695 Median :3.325 Median :17.71 #Mean :20.09 Mean :6.188 Mean :230.7 Mean :146.7 Mean :3.597 Mean :3.217 Mean :17.85 #3rd Qu.:22.80 3rd Qu.:8.000 3rd Qu.:326.0 3rd Qu.:180.0 3rd Qu.:3.920 3rd Qu.:3.610 3rd Qu.:18.90 #Max. :33.90 Max. :8.000 Max. :472.0 Max. :335.0 Max. :4.930 Max. :5.424 Max. :22.90 #vs am gear carb #Min. :0.0000 Min. :0.0000 Min. :3.000 Min. :1.000 #1st Qu.:0.0000 1st Qu.:0.0000 1st Qu.:3.000 1st Qu.:2.000 #Median :0.0000 Median :0.0000 Median :4.000 Median :2.000 #Mean :0.4375 Mean :0.4062 Mean :3.688 Mean :2.812 #3rd Qu.:1.0000 3rd Qu.:1.0000 3rd Qu.:4.000 3rd Qu.:4.000 #Max. :1.0000 Max. :1.0000 Max. :5.000 Max. :8.000 df<- mtcars # asigno los datos a df para recordar mas facilmente el nombre del dataset view(df) print(df) # I look for the correlation between each of the variables vs to "mpg" cor.test(df$cyl, df$mpg) # -0.852162 --> relevant cor.test(df$disp, df$mpg) # -0.8475514 --> relevant cor.test(df$hp, df$mpg) # -0.7761684 cor.test(df$drat, df$mpg) # 0.6811719 cor.test(df$wt, df$mpg) # -0.8676594 --> relevant cor.test(df$qsec, df$mpg) # 0.418684 cor.test(df$vs, df$mpg) # 0.6640389 cor.test(df$am, df$mpg) # 0.5998324 cor.test(df$gear, df$mpg) # 0.4802848 cor.test(df$carb, df$mpg) # -0.5509251 # I build the "lm" with all the variables to further explore the data model <- lm(mpg ~ cyl + disp + hp + drat + wt + qsec + vs + am + gear + carb, df, na.action = na.exclude) anova(model) # Response: mpg # Df Sum Sq Mean Sq F value Pr(>F) # cyl 1 817.71 817.71 116.4245 5.034e-10 *** # disp 1 37.59 37.59 5.3526 0.030911 * # hp 1 9.37 9.37 1.3342 0.261031 # drat 1 16.47 16.47 2.3446 0.140644 # wt 1 77.48 77.48 11.0309 0.003244 ** # qsec 1 3.95 3.95 0.5623 0.461656 # vs 1 0.13 0.13 0.0185 0.893173 # am 1 14.47 14.47 2.0608 0.165858 # gear 1 0.97 0.97 0.1384 0.713653 # carb 1 0.41 0.41 0.0579 0.812179 # Residuals 21 147.49 7.02 # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 summary(model) # Call: # lm(formula = mpg ~ cyl + disp + hp + drat + wt + qsec + vs + # am + gear + carb, data = df, na.action = na.exclude) # # Residuals: # Min 1Q Median 3Q Max # -3.4506 -1.6044 -0.1196 1.2193 4.6271 # # Coefficients: # Estimate Std. Error t value Pr(>|t|) # (Intercept) 12.30337 18.71788 0.657 0.5181 # cyl -0.11144 1.04502 -0.107 0.9161 # disp 0.01334 0.01786 0.747 0.4635 # hp -0.02148 0.02177 -0.987 0.3350 # drat 0.78711 1.63537 0.481 0.6353 # wt -3.71530 1.89441 -1.961 0.0633 . # qsec 0.82104 0.73084 1.123 0.2739 # vs 0.31776 2.10451 0.151 0.8814 # am 2.52023 2.05665 1.225 0.2340 # gear 0.65541 1.49326 0.439 0.6652 # carb -0.19942 0.82875 -0.241 0.8122 # --- # Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 # # Residual standard error: 2.65 on 21 degrees of freedom # Multiple R-squared: 0.869, Adjusted R-squared: 0.8066 # F-statistic: 13.93 on 10 and 21 DF, p-value: 3.793e-07 #I build the graph to visualize the data obtained from the "lm" par(mfrow=c(2,2)) plot (model, pch=16, col="blue") #as you can see everything is very basic, now I am trying to start the solution #To the question, if you can give me a light on how to start, I would appreciate it a lot, I know almost nothing about R # I hope to learn as much as I can. Thank you very much in advance for any help. 

1 Answer

If I recall to your problems: this tips is good to explore what is combinat::combn or any kind of functions out there in R you want to explore:

  1. type ?combinat::combn -> this will open up documentation about how to use combn function in a standard procedure:

  2. since combn is a function, you can see what is inside of that functions, with typing combn only

  3. See combn working examples in the documentation, like:

    combn(letters[1:4], 2)

    combn(c(1,1,1,1,2,2,2,3,3,4), 3, tabulate, nbins = 4)

and by looking to your problem pattern combination between variables can also be made using expand.grid

examples: expand.grid(letters[1:5], letters[1:5]) will produce combination between first (a-e) letters and second (a-e) letters

So if you want to apply this to mtcars dataset, this also works:

expand.grid(colnames(mtcars), colnames(mtcars)) 
2

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.