How To Represent Variables As Function For Model In R

Introduction

 
In this article I am going to demonstrate how to select and represent relevant variables as a function for a model in R. Using a combination of function aggregate with certain variables and dataset, we can define the relevant variables from a dataset which can be used to create models in R. Using combination function, we can combine several important variables to create a model and also how define how a particular model will perform its tasks.
 

Appending variables into model

 
In order to append number of variables together, we can use a combination function along with mathematical operators so as to combine two or more than two variables together. For example, we can use addition operator inside aggregate function to insert a variable for the creation of a model. To create a model in R, we can use various mathematical operators to insert relevant variables and to remove unnecessary variables.
 
We can use statistics along with arithmetic operators in lots of functions in R. One such function is the aggregate() function in which we append different relevant variables to create a model. We can use statistical formulas along with arithmetic operators in lots of functions in R. one of such functions is the aggregate() function in which we append different relevant variables to create a model.
 
Now I will demonstrate the use of cluster function to incorporate several variables together. We will be using gscars dataset to demonstrate the use of aggregate function.
  1. > gscars  
  2.                      mg cyl  disp  hp drat    wt  qsec vs am gr carb  
  3. Mazda RX4           21.0   6 160.0 110 3.90 2.620 16.46  0  1    4    4  
  4. Mazda RX4 Wag       21.0   6 160.0 110 3.90 2.875 17.02  0  1    4    4  
  5. Datsun 710          22.8   4 108.0  93 3.85 2.320 18.61  1  1    4    1  
  6. Hornet 4 Drive      21.4   6 258.0 110 3.08 3.215 19.44  1  0    3    1  
  7. Hornet Sportabout   18.7   8 360.0 175 3.15 3.440 17.02  0  0    3    2  
  8. Valiant             18.1   6 225.0 105 2.76 3.460 20.22  1  0    3    1  
  9. Duster 360          14.3   8 360.0 245 3.21 3.570 15.84  0  0    3    4  
  10. Merc 240D           24.4   4 146.7  62 3.69 3.190 20.00  1  0    4    2  
  11. Merc 230            22.8   4 140.8  95 3.92 3.150 22.90  1  0    4    2  
  12. Merc 280            19.2   6 167.6 123 3.92 3.440 18.30  1  0    4    4  
  13. Merc 280C           17.8   6 167.6 123 3.92 3.440 18.90  1  0    4    4  
  14. Merc 450SE          16.4   8 275.8 180 3.07 4.070 17.40  0  0    3    3  
  15. Merc 450SL          17.3   8 275.8 180 3.07 3.730 17.60  0  0    3    3  
  16. Merc 450SLC         15.2   8 275.8 180 3.07 3.780 18.00  0  0    3    3  
  17. Cadillac Fleetwood  10.4   8 472.0 205 2.93 5.250 17.98  0  0    3    4  
  18. Lincoln Continental 10.4   8 460.0 215 3.00 5.424 17.82  0  0    3    4  
  19. Chrysler Imperial   14.7   8 440.0 230 3.23 5.345 17.42  0  0    3    4  
  20. Fiat 128            32.4   4  78.7  66 4.08 2.200 19.47  1  1    4    1  
  21. Honda Civic         30.4   4  75.7  52 4.93 1.615 18.52  1  1    4    2  
  22. Toyota Corolla      33.9   4  71.1  65 4.22 1.835 19.90  1  1    4    1  
  23. Toyota Corona       21.5   4 120.1  97 3.70 2.465 20.01  1  0    3    1  
  24. Dodge Challenger    15.5   8 318.0 150 2.76 3.520 16.87  0  0    3    2  
  25. AMC Javelin         15.2   8 304.0 150 3.15 3.435 17.30  0  0    3    2  
  26. Camaro Z28          13.3   8 350.0 245 3.73 3.840 15.41  0  0    3    4  
  27. Pontiac Firebird    19.2   8 400.0 175 3.08 3.845 17.05  0  0    3    2  
  28. Fiat X1-9           27.3   4  79.0  66 4.08 1.935 18.90  1  1    4    1  
  29. Porsche 914-2       26.0   4 120.3  91 4.43 2.140 16.70  0  1    5    2  
  30. Lotus Europa        30.4   4  95.1 113 3.77 1.513 16.90  1  1    5    2  
  31. Ford Pantera L      15.8   8 351.0 264 4.22 3.170 14.50  0  1    5    4  
  32. Ferrari Dino        19.7   6 145.0 175 3.62 2.770 15.50  0  1    5    6  
  33. Maserati Bora       15.0   8 301.0 335 3.54 3.570 14.60  0  1    5    8  
  34. Volvo 142E          21.4   4 121.0 109 4.11 2.780 18.60  1  1    4    2  
  35. >  
We will be using ~ operator inside aggregate function. For example if there are two variables named a and b, then a ~ b means that aggregate function will create a model having a as a function of b.
  1. > data = gscars  
  2. > aggregate(mg ~ gr, data = data, mean)  
  3.   gr      mg  
  4. 1    2 18.30667  
  5. 2    6 21.33333  
  6. 3    4 26.48700  
In the above aggregate function, there are three arguments. First argument in the formula indicates that aggregate function represents mg as a function of gr and the second argument is the variable depicting dataset. Using above code, aggregate function creates a model in which mg variable is appended with gr variable. The mean is also calculated.
  1. > aggregate(mg ~ disp, data = data, mean)  
  2.     disp  mg  
  3. 1   71.1 33.9  
  4. 2   75.7 30.4  
  5. 3   78.7 32.4  
  6. 4   79.0 27.3  
  7. 5   95.1 30.4  
  8. 6  108.0 22.8  
  9. 7  120.1 21.5  
  10. 8  120.3 26.0  
  11. 9  121.0 21.4  
  12. 10 140.8 22.8  
  13. 11 145.0 19.7  
  14. 12 146.7 24.4  
  15. 13 160.0 21.0  
  16. 14 167.6 18.5  
  17. 15 225.0 18.1  
  18. 16 258.0 21.4  
  19. 17 275.8 16.3  
  20. 18 301.0 15.0  
  21. 19 304.0 15.2  
  22. 20 318.0 15.5  
  23. 21 350.0 13.3  
  24. 22 351.0 15.8  
  25. 23 360.0 16.5  
  26. 24 400.0 19.2  
  27. 25 440.0 14.7  
  28. 26 460.0 10.4  
  29. 27 472.0 10.4  
In the above aggregate function, there are three arguments. The first argument in the formula indicates that aggregate function represents mg as a function of disp in the model and also calculates the mean. The second argument is the variable depicting dataset. Using above code, aggregate function creates a model in which mg variable is appended with disp variable.
  1. > aggregate(mg ~ hp, data = data, mean)  
  2.     hp      mg  
  3. 1   52 30.40000  
  4. 2   62 24.40000  
  5. 3   65 33.90000  
  6. 4   66 29.85000  
  7. 5   91 26.00000  
  8. 6   93 22.80000  
  9. 7   95 22.80000  
  10. 8   97 21.50000  
  11. 9  105 18.10000  
  12. 10 109 21.40000  
  13. 11 110 21.13333  
  14. 12 113 30.40000  
  15. 13 123 18.50000  
  16. 14 150 15.35000  
  17. 15 175 19.20000  
  18. 16 180 16.30000  
  19. 17 205 10.40000  
  20. 18 215 10.40000  
  21. 19 230 14.70000  
  22. 20 245 13.80000  
  23. 21 264 15.80000  
  24. 22 335 15.00000  
In the above aggregate function, there are three arguments. The first argument in the formula indicates that aggregate function represents mg as a function of hp and the second argument is the variable depicting dataset. Using above code, aggregate function creates a model in which mg variable is appended with hp variable.
  1. > aggregate(mg ~ cyl, data = data, mean)  
  2.   cyl      mg  
  3. 1   4 26.66364  
  4. 2   6 19.74286  
  5. 3   8 15.10000  
In the above aggregate function, there are three arguments. The first argument in the formula indicates that aggregate function represents mg as a function of cyl and the second argument is the variable depicting dataset. Using the above code, aggregate function creates a model in which mg variable is appended with cyl variable.
  1. > aggregate(mg ~ wt, data = data, mean)  
  2.       wt      mg  
  3. 1  1.513 30.40000  
  4. 2  1.615 30.40000  
  5. 3  1.835 33.90000  
  6. 4  1.935 27.30000  
  7. 5  2.140 26.00000  
  8. 6  2.200 32.40000  
  9. 7  2.320 22.80000  
  10. 8  2.465 21.50000  
  11. 9  2.620 21.00000  
  12. 10 2.770 19.70000  
  13. 11 2.780 21.40000  
  14. 12 2.875 21.00000  
  15. 13 3.150 22.80000  
  16. 14 3.170 15.80000  
  17. 15 3.190 24.40000  
  18. 16 3.215 21.40000  
  19. 17 3.435 15.20000  
  20. 18 3.440 18.56667  
  21. 19 3.460 18.10000  
  22. 20 3.520 15.50000  
  23. 21 3.570 14.65000  
  24. 22 3.730 17.30000  
  25. 23 3.780 15.20000  
  26. 24 3.840 13.30000  
  27. 25 3.845 19.20000  
  28. 26 4.070 16.40000  
  29. 27 5.250 10.40000  
  30. 28 5.345 14.70000  
  31. 29 5.424 10.40000  
In the above aggregate function, there are three arguments. The first argument in the formula indicates that aggregate function represents mg as a function of wt and the second argument is the variable depicting dataset. Using above code, aggregate function creates a model in which mg variable is appended with wt variable.
  1. > aggregate(mg ~ qsec, data = data, mean)  
  2.     qsec   mg  
  3. 1  14.50 15.80  
  4. 2  14.60 15.00  
  5. 3  15.41 13.30  
  6. 4  15.50 19.70  
  7. 5  15.84 14.30  
  8. 6  16.46 21.00  
  9. 7  16.70 26.00  
  10. 8  16.87 15.50  
  11. 9  16.90 30.40  
  12. 10 17.02 19.85  
  13. 11 17.05 19.20  
  14. 12 17.30 15.20  
  15. 13 17.40 16.40  
  16. 14 17.42 14.70  
  17. 15 17.60 17.30  
  18. 16 17.82 10.40  
  19. 17 17.98 10.40  
  20. 18 18.00 15.20  
  21. 19 18.30 19.20  
  22. 20 18.52 30.40  
  23. 21 18.60 21.40  
  24. 22 18.61 22.80  
  25. 23 18.90 22.55  
  26. 24 19.44 21.40  
  27. 25 19.47 32.40  
  28. 26 19.90 33.90  
  29. 27 20.00 24.40  
  30. 28 20.01 21.50  
  31. 29 20.22 18.10  
  32. 30 22.90 22.80  
  33. >  
In the above aggregate function, there are three arguments. The first argument in the formula indicates that aggregate function represents mg as a function of qsec calculating the mean and the second argument is the variable depicting dataset. Using above code, aggregate function creates a model in which mg variable is appended with qsec variable.
 

Summary

 
In this article I demonstrated how to select and represent relevant variables as a function for a model in R. Using a combination of function aggregate with certain variables and dataset, we can define the relevant variables from a dataset which can be used to create models in R. 


Similar Articles