-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCh02_Application_SL_R.Rmd
427 lines (281 loc) · 8.94 KB
/
Ch02_Application_SL_R.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
---
title: "Statistical Learning Application"
date: "2024-1-18"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,message=FALSE,fig.align="center",fig.width=7,fig.height=2.5)
pacman::p_load(
car
, ggplot2
, ggExtra
, reshape2
, corrplot
, RColorBrewer
, lubridate
, AmesHousing
)
```
```{css}
.btn {
border-width: 0 0px 0px 0px;
font-weight: normal;
text-transform: ;
}
.btn-default {
color: #2ecc71;
background-color: #ffffff;
border-color: #ffffff;
}
```
# Class Workbook {.tabset .tabset-fade .tabset-pills}
This is coding portion of your class workbook. Please go through the sections below
## Welcome to MA679
Every module of the course will have a GitHub class that contains the chapter lab from your textbook and this application material, like your workbook/homework. You are expected to work on these materials during the class, but what you don't finish will become your homework. At the end of each class, you must commit your work as proof of your class participation.
***
## In class activity
### Ames Housing data
In our first class, we will look at the Ames Housing data.
Please install `AmesHousing` package and look at the raw data.
```{r}
library(AmesHousing)
?ames_raw
```
The outcome variable of interest is `SalePrice` and you have a lot of features for each home.
A sample of them include
- `Year Built`: Original construction date
- `Lot Frontage`: Linear feet of street connected to the property
- `Lot Area`: Lot size in square feet
- `Neighborhood`: Physical locations within Ames city limits (map available)
- `Overall Qual`: Rates the overall material and finish of the house
- `Overall Cond`: Rates the overall condition of the house
- `Total Bsmt SF`: Total square feet of the basement area
- `1st Flr SF`: First Floor square feet
- `2nd Flr SF`: Second floor square feet
- `Gr Liv Area`: Above grade (ground) living area square feet
- `Bedroom`: Bedrooms above grade (does NOT include basement bedrooms)
- `Kitchen`: Kitchens above grade
- `KitchenQual`: Kitchen quality
- `TotRmsAbvGrd`: Total rooms above grade (does not include bathrooms)
- `Bsmt Full Bath`: Basement full bathrooms
- `Bsmt Half Bath`: Basement half bathrooms
- `Full Bath`: Full bathrooms above grade
- `Half Bath`: Half baths above grade, etc.
### Questions
- How many observations and features do you have in the data?
Your code:
```{r,echo=TRUE}
#
#
```
Your answer:
~~~
Please write your answer in full sentences.
~~~
- Please make some EDA figures to explore some of the most likely features associated with `SalePrice`.
Your code:
```{r,echo=TRUE}
#
#
```
Your answer:
~~~
Please write your answer in full sentences.
~~~
- Are there any structures in the data? Obvious correlations to be aware of?
Your code:
```{r,echo=TRUE}
#
#
```
Your answer:
~~~
Please write your answer in full sentences.
~~~
***
## Problem Set
### [Required] Auto Data
This exercise involves the `Auto` data set studied in the lab. Make sure that the missing values have been removed from the data.
(a) Which of the predictors are quantitative, and which are qualitative?
Your code:
```{r,echo=TRUE}
#
#
```
Your answer:
~~~
Please write your answer in full sentences.
~~~
(b) What is the range of each quantitative predictor? You can answer this using the range() function. range()
Your code:
```{r,echo=TRUE}
#
#
```
Your answer:
~~~
Please write your answer in full sentences.
~~~
(c) What is the mean and standard deviation of each quantitative predictor?
Your code:
```{r,echo=TRUE}
#
#
```
Your answer:
~~~
Please write your answer in full sentences.
~~~
(d) Now remove the 10th through 85th observations. What is the range, mean, and standard deviation of each predictor in the subset of the data that remains?
Your code:
```{r,echo=TRUE}
#
#
```
Your answer:
~~~
Please write your answer in full sentences.
~~~
(e) Using the full data set, investigate the predictors graphically, using scatterplots or other tools of your choice. Create some plots highlighting the relationships among the predictors. Comment
on your findings.
Your code:
```{r,echo=TRUE}
#
#
```
Your answer:
~~~
Please write your answer in full sentences.
~~~
(f) Suppose that we wish to predict gas mileage (mpg) based on the other variables. Do your plots suggest that any other variables might be helpful in predicting mpg? Justify your answer.
Your code:
```{r,echo=TRUE}
#
#
```
Your answer:
~~~
Please write your answer in full sentences.
~~~
### [Optional] College Data
This exercise relates to the `College` data set, found in the file College.csv on the book website. It contains several variables for 777 different universities and colleges in the US. The
variables are
- `Private` : Public/private indicator
- `Apps` : Number of applications received
- `Accept` : Number of applicants accepted
- `Enroll` : Number of new students enrolled
- `Top10perc` : New students from the top 10% of high school class
- `Top25perc` : New students from the top 25% of high school class
- `F.Undergrad` : Number of full-time undergraduates
- `P.Undergrad` : Number of part-time undergraduates
- `Outstate` : Out-of-state tuition
- `Room.Board` : Room and board costs
- `Books` : Estimated book costs
- `Personal` : Estimated personal spending
- `PhD` : Percent of faculty with Ph.D.’s
- `Terminal` : Percent of faculty with a terminal degree
- `S.F.Ratio` : Student/faculty ratio
- `perc.alumni` : Percent of alumni who donate
- `Expend` : Instructional expenditure per student
- `Grad.Rate` : Graduation rate
Before reading the data into R, it can be viewed in Excel or a text editor.
(a) Use the `read.csv()` function to read the data into R. Call the loaded data college. Make sure you have the directory set to the correct location for the data.
Your code:
```{r,echo=TRUE}
#
#
```
Your answer:
~~~
Please write your answer in full sentences.
~~~
(b) Look at the data using the `View()` function. You should notice that the first column is just the name of each university. We don’t want R to treat this as data. However, it may be handy to have these names for later. Try the following commands:
```{r,eval=FALSE}
> rownames(college) <- college[, 1]
> View(college)
```
You should see that there is now a row.names column with the name of each university recorded. This means that R has given each row a name corresponding to the appropriate university. R will not try to perform calculations on the row names. However, we still need to eliminate the first column in the data where the names are stored. Try
```{r,eval=FALSE}
> college <- college[, -1]
> View(college)
```
Now you should see that the first data column is Private. Note that another column labeled row.names now appears before the Private column. However, this is not a data column the name that R is giving to each row.
Your code:
```{r,echo=TRUE}
#
#
```
Your answer:
~~~
Please write your answer in full sentences.
~~~
(c)
i. Use the summary() function to produce a numerical summary of the variables in the data set.
Your code:
```{r,echo=TRUE}
#
#
```
Your answer:
~~~
Please write your answer in full sentences.
~~~
ii. Use the pairs() function to produce a scatterplot matrix of the first ten columns or variables of the data. Recall that you can reference the first ten columns of a matrix A using
`A[,1:10]`.
Your code:
```{r,echo=TRUE}
#
#
```
Your answer:
~~~
Please write your answer in full sentences.
~~~
iii. Use the plot() function to produce side-by-side boxplots of Outstate versus Private.
Your code:
```{r,echo=TRUE}
#
#
```
Your answer:
~~~
Please write your answer in full sentences.
~~~
iv. Create a new qualitative variable, called Elite, by binning the Top10perc variable. We will divide universities into two groups based on whether or not the proportion of students coming from the top 10% of their high school classes exceeds 50%.
```{r,eval=FALSE}
> Elite <- rep("No", nrow(college))
> Elite[college$Top10perc > 50] <- "Yes"
> Elite <- as.factor(Elite)
> college <- data.frame(college , Elite)
```
Use the `summary()` function to see how many elite universities there are. Now use the `plot()` function to produce side-by-side boxplots of Outstate versus Elite.
Your code:
```{r,echo=TRUE}
#
#
```
Your answer:
~~~
Please write your answer in full sentences.
~~~
v. Use the hist() function to produce some histograms with differing numbers of bins for a few of the quantitative variables. You may find the command `par(mfrow = c(2, 2))`
useful: it will divide the print window into four regions so that four plots can be made simultaneously. Modifying the arguments to this function will divide the screen in other ways.
Your code:
```{r,echo=TRUE}
#
#
```
Your answer:
~~~
Please write your answer in full sentences.
~~~
vi. Continue exploring the data, and provide a summary of what you discover.
Your code:
```{r,echo=TRUE}
#
#
```
Your answer:
~~~
Please write your answer in full sentences.
~~~