Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

ISLR Chapter 2 Statistical Learning Lab Manual, Exercises of Statistics

Introduction to Statistical Learning (James/Witten/Hastie/Tibshirani)

Typology: Exercises

2020/2021

Uploaded on 05/26/2021

ekambar
ekambar 🇺🇸

4.7

(23)

265 documents

1 / 7

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Chapter 2 - Statistical Learning
Lab Solution
1 Problem 8
(a). Use the read.csv() function to read the data into R. Call the loaded data college. Make sure that you
have the directory set to the correct location for the data.
college=read.csv("College.csv")
(b). Look at the data using the fix() function. You should notice that the first column is just the name of
each university. We don’t really want R to treat this as data. However, it may be handy to have these
names for later. Try the following commands:
fix(college)
This will bring up a data viewer. In R-studio you can also use the "View()" function or click the dataset name in the
Environment tab in the top right pane
rownames(college)=college[,1]
This line tells R to use the first column in the dataset(college names) as the rownames. Have a look at the fix() function
output to see this.
college=college[,-1]
We then remove the first column of data, since we do not need the school names as a variable. Row names are still
there for us but for reference only.
(c). (i) Use the summary() function to produce a numerical summary of the variables in the data set.
summary(college)
## Private Apps Accept Enroll Top10perc Top25perc
## No :212 Min. : 81 Min. : 72 Min. : 35 Min. : 1.00 Min. : 9.0
## Yes:565 1st Qu.: 776 1st Qu.: 604 1st Qu.: 242 1st Qu.:15.00 1st Qu.: 41.0
## Median : 1558 Median : 1110 Median : 434 Median :23.00 Median : 54.0
## Mean : 3002 Mean : 2019 Mean : 780 Mean :27.56 Mean : 55.8
## 3rd Qu.: 3624 3rd Qu.: 2424 3rd Qu.: 902 3rd Qu.:35.00 3rd Qu.: 69.0
## Max. :48094 Max. :26330 Max. :6392 Max. :96.00 Max. :100.0
## F.Undergrad P.Undergrad Outstate Room.Board Books
## Min. : 139 Min. : 1.0 Min. : 2340 Min. :1780 Min. : 96.0
## 1st Qu.: 992 1st Qu.: 95.0 1st Qu.: 7320 1st Qu.:3597 1st Qu.: 470.0
## Median : 1707 Median : 353.0 Median : 9990 Median :4200 Median : 500.0
## Mean : 3700 Mean : 855.3 Mean :10441 Mean :4358 Mean : 549.4
## 3rd Qu.: 4005 3rd Qu.: 967.0 3rd Qu.:12925 3rd Qu.:5050 3rd Qu.: 600.0
## Max. :31643 Max. :21836.0 Max. :21700 Max. :8124 Max. :2340.0
## Personal PhD Terminal S.F.Ratio perc.alumni
## Min. : 250 Min. : 8.00 Min. : 24.0 Min. : 2.50 Min. : 0.00
## 1st Qu.: 850 1st Qu.: 62.00 1st Qu.: 71.0 1st Qu.:11.50 1st Qu.:13.00
## Median :1200 Median : 75.00 Median : 82.0 Median :13.60 Median :21.00
## Mean :1341 Mean : 72.66 Mean : 79.7 Mean :14.09 Mean :22.74
1
pf3
pf4
pf5

Partial preview of the text

Download ISLR Chapter 2 Statistical Learning Lab Manual and more Exercises Statistics in PDF only on Docsity!

Chapter 2 - Statistical Learning

Lab Solution

1 Problem 8

(a). Use the read.csv() function to read the data into R. Call the loaded data college. Make sure that you have the directory set to the correct location for the data.

college= read.csv ("College.csv")

(b). Look at the data using the fix() function. You should notice that the first column is just the name of each university. We don’t really want R to treat this as data. However, it may be handy to have these names for later. Try the following commands :

fix (college)

This will bring up a data viewer. In R-studio you can also use the "View()" function or click the dataset name in the Environment tab in the top right pane

rownames (college)=college[,1]

This line tells R to use the first column in the dataset(college names) as the rownames. Have a look at the fix() function output to see this.

college=college[,-1]

We then remove the first column of data , since we do not need the school names as a variable. Row names are still there for us but for reference only.

(c). (i) Use the summary() function to produce a numerical summary of the variables in the data set.

summary (college)

Private Apps Accept Enroll Top10perc Top25perc

No :212 Min. : 81 Min. : 72 Min. : 35 Min. : 1.00 Min. : 9.

Yes:565 1st Qu.: 776 1st Qu.: 604 1st Qu.: 242 1st Qu.:15.00 1st Qu.: 41.

Median : 1558 Median : 1110 Median : 434 Median :23.00 Median : 54.

Mean : 3002 Mean : 2019 Mean : 780 Mean :27.56 Mean : 55.

3rd Qu.: 3624 3rd Qu.: 2424 3rd Qu.: 902 3rd Qu.:35.00 3rd Qu.: 69.

Max. :48094 Max. :26330 Max. :6392 Max. :96.00 Max. :100.

F.Undergrad P.Undergrad Outstate Room.Board Books

Min. : 139 Min. : 1.0 Min. : 2340 Min. :1780 Min. : 96.

1st Qu.: 992 1st Qu.: 95.0 1st Qu.: 7320 1st Qu.:3597 1st Qu.: 470.

Median : 1707 Median : 353.0 Median : 9990 Median :4200 Median : 500.

Mean : 3700 Mean : 855.3 Mean :10441 Mean :4358 Mean : 549.

3rd Qu.: 4005 3rd Qu.: 967.0 3rd Qu.:12925 3rd Qu.:5050 3rd Qu.: 600.

Max. :31643 Max. :21836.0 Max. :21700 Max. :8124 Max. :2340.

Personal PhD Terminal S.F.Ratio perc.alumni

Min. : 250 Min. : 8.00 Min. : 24.0 Min. : 2.50 Min. : 0.

1st Qu.: 850 1st Qu.: 62.00 1st Qu.: 71.0 1st Qu.:11.50 1st Qu.:13.

Median :1200 Median : 75.00 Median : 82.0 Median :13.60 Median :21.

Mean :1341 Mean : 72.66 Mean : 79.7 Mean :14.09 Mean :22.

3rd Qu.:1700 3rd Qu.: 85.00 3rd Qu.: 92.0 3rd Qu.:16.50 3rd Qu.:31.

Max. :6800 Max. :103.00 Max. :100.0 Max. :39.80 Max. :64.

Expend Grad.Rate

Min. : 3186 Min. : 10.

1st Qu.: 6751 1st Qu.: 53.

Median : 8377 Median : 65.

Mean : 9660 Mean : 65.

3rd Qu.:10830 3rd Qu.: 78.

Max. :56233 Max. :118.

Among other things, we can see that 565 of the 777 schools in the dataset are private, out of state tuition is $10,441 on average, there exists a school for which somehow 118% of the students graduate, as well as one for which 0% of alumni donate.

Anomolies like the graduation rate happen all the time. This highlights the importance of always needing to explore the data.

(ii) Use the pairs() function to produce a scatterplot matrix of the first ten columns or variables of the data.

pairs (college[,1:10])

No Yes

Private schools are more expensive!

(iv) Create a new qualitative variable, called Elite, by binning the Top10perc variable. We are going to 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%.

Elite = rep ("No", nrow (college)) Elite[college$Top10perc>50] = "Yes" Elite = as.factor (Elite) college = data.frame (college, Elite) summary (college$Elite)

No Yes

699 78

plot (college$Elite, college$Outstate)

No Yes

(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.

par (mfrow= c (2,2)) hist (college$Apps) hist (college$perc.alumni, col=2) hist (college$S.F.Ratio, col=3, breaks=10) hist (college$Expend, breaks=100)

college$Outstate

college$Grad.Rate

college$Accept/college$Apps

college$S.F.Ratio

college$Top10perc

college$Grad.Rate