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.
- > gscars
- mg cyl disp hp drat wt qsec vs am gr carb
- Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
- Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
- Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
- Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
- Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
- Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
- Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
- Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
- Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
- Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
- Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
- Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
- Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
- Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
- Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
- Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
- Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
- Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
- Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
- Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
- Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
- Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
- AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
- Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
- Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
- Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
- Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
- Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
- Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
- Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
- Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
- Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2
- >
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.
- > data = gscars
- > aggregate(mg ~ gr, data = data, mean)
- gr mg
- 1 2 18.30667
- 2 6 21.33333
- 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.
- > aggregate(mg ~ disp, data = data, mean)
- disp mg
- 1 71.1 33.9
- 2 75.7 30.4
- 3 78.7 32.4
- 4 79.0 27.3
- 5 95.1 30.4
- 6 108.0 22.8
- 7 120.1 21.5
- 8 120.3 26.0
- 9 121.0 21.4
- 10 140.8 22.8
- 11 145.0 19.7
- 12 146.7 24.4
- 13 160.0 21.0
- 14 167.6 18.5
- 15 225.0 18.1
- 16 258.0 21.4
- 17 275.8 16.3
- 18 301.0 15.0
- 19 304.0 15.2
- 20 318.0 15.5
- 21 350.0 13.3
- 22 351.0 15.8
- 23 360.0 16.5
- 24 400.0 19.2
- 25 440.0 14.7
- 26 460.0 10.4
- 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.
- > aggregate(mg ~ hp, data = data, mean)
- hp mg
- 1 52 30.40000
- 2 62 24.40000
- 3 65 33.90000
- 4 66 29.85000
- 5 91 26.00000
- 6 93 22.80000
- 7 95 22.80000
- 8 97 21.50000
- 9 105 18.10000
- 10 109 21.40000
- 11 110 21.13333
- 12 113 30.40000
- 13 123 18.50000
- 14 150 15.35000
- 15 175 19.20000
- 16 180 16.30000
- 17 205 10.40000
- 18 215 10.40000
- 19 230 14.70000
- 20 245 13.80000
- 21 264 15.80000
- 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.
- > aggregate(mg ~ cyl, data = data, mean)
- cyl mg
- 1 4 26.66364
- 2 6 19.74286
- 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.
- > aggregate(mg ~ wt, data = data, mean)
- wt mg
- 1 1.513 30.40000
- 2 1.615 30.40000
- 3 1.835 33.90000
- 4 1.935 27.30000
- 5 2.140 26.00000
- 6 2.200 32.40000
- 7 2.320 22.80000
- 8 2.465 21.50000
- 9 2.620 21.00000
- 10 2.770 19.70000
- 11 2.780 21.40000
- 12 2.875 21.00000
- 13 3.150 22.80000
- 14 3.170 15.80000
- 15 3.190 24.40000
- 16 3.215 21.40000
- 17 3.435 15.20000
- 18 3.440 18.56667
- 19 3.460 18.10000
- 20 3.520 15.50000
- 21 3.570 14.65000
- 22 3.730 17.30000
- 23 3.780 15.20000
- 24 3.840 13.30000
- 25 3.845 19.20000
- 26 4.070 16.40000
- 27 5.250 10.40000
- 28 5.345 14.70000
- 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.
- > aggregate(mg ~ qsec, data = data, mean)
- qsec mg
- 1 14.50 15.80
- 2 14.60 15.00
- 3 15.41 13.30
- 4 15.50 19.70
- 5 15.84 14.30
- 6 16.46 21.00
- 7 16.70 26.00
- 8 16.87 15.50
- 9 16.90 30.40
- 10 17.02 19.85
- 11 17.05 19.20
- 12 17.30 15.20
- 13 17.40 16.40
- 14 17.42 14.70
- 15 17.60 17.30
- 16 17.82 10.40
- 17 17.98 10.40
- 18 18.00 15.20
- 19 18.30 19.20
- 20 18.52 30.40
- 21 18.60 21.40
- 22 18.61 22.80
- 23 18.90 22.55
- 24 19.44 21.40
- 25 19.47 32.40
- 26 19.90 33.90
- 27 20.00 24.40
- 28 20.01 21.50
- 29 20.22 18.10
- 30 22.90 22.80
- >
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.