stat 4510/7510 homework 2 - university of...

8
Stat 4510/7510 1/8 . Stat 4510/7510 Homework 2 Instructions: Please list your name and student number clearly. In order to receive credit for a problem, your solution must show sufficient details so that the grader can determine how you obtained your answer. 1. The dataset seeds.csv consists of measurements of geometrical properties of 210 wheat kernels belonging to three different varieties (Kama, Rosa and Canadian). High quality visualization of the internal kernel structure was detected using a soft X-ray technique to construct the following seven, real-valued attributes: Area Perimeter Compactness C =4π A P 2 Length of kernel Width of kernel Asymmetry coefficient Length of kernel groove (a). Use the read.csv() function to read the data into R. Redefine the “Type” variable from (1,2,3) to (Kama, Rosa, Canadian), using, for example, seeds$Type[which(seeds$Type==1)]="Kama" In addition, ensure that R knows that this variable is a factor with the line seeds$Type = factor(seeds$Type) seeds=read.csv("seeds.csv") seeds$Type[which(seeds$Type==1)]="Kama" seeds$Type[which(seeds$Type==2)]="Rosa" seeds$Type[which(seeds$Type==3)]="Canadian" seeds$Type = factor(seeds$Type) (b). Produce a summary table of the dataset. 1

Upload: others

Post on 11-Mar-2020

8 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Stat 4510/7510 Homework 2 - University of Missourifaculty.missouri.edu/~schliepe/ewExternalFiles/hw2_solutions-1.pdfbelonging to three di erent varieties (Kama, Rosa and Canadian)

Stat 4510/7510 1/8

.

Stat 4510/7510 Homework 2

• Instructions:

– Please list your name and student number clearly.

– In order to receive credit for a problem, your solution must show sufficient details sothat the grader can determine how you obtained your answer.

1.

The dataset seeds.csv consists of measurements of geometrical properties of 210 wheat kernelsbelonging to three different varieties (Kama, Rosa and Canadian). High quality visualization ofthe internal kernel structure was detected using a soft X-ray technique to construct the followingseven, real-valued attributes:

• Area

• Perimeter

• Compactness C = 4π AP 2

• Length of kernel

• Width of kernel

• Asymmetry coefficient

• Length of kernel groove

(a). Use the read.csv() function to read the data into R. Redefine the “Type” variable from(1,2,3) to (Kama, Rosa, Canadian), using, for example,

seeds$Type[which(seeds$Type==1)]="Kama"

In addition, ensure that R knows that this variable is a factor with the line

seeds$Type = factor(seeds$Type)

seeds=read.csv("seeds.csv")

seeds$Type[which(seeds$Type==1)]="Kama"

seeds$Type[which(seeds$Type==2)]="Rosa"

seeds$Type[which(seeds$Type==3)]="Canadian"

seeds$Type = factor(seeds$Type)

(b). Produce a summary table of the dataset.

1

Page 2: Stat 4510/7510 Homework 2 - University of Missourifaculty.missouri.edu/~schliepe/ewExternalFiles/hw2_solutions-1.pdfbelonging to three di erent varieties (Kama, Rosa and Canadian)

Stat 4510/7510 2/8

summary(seeds)

## Area perimeter compactness length.of.kernel

## Min. :10.59 Min. :12.41 Min. :0.8081 Min. :4.899

## 1st Qu.:12.27 1st Qu.:13.45 1st Qu.:0.8569 1st Qu.:5.262

## Median :14.36 Median :14.32 Median :0.8734 Median :5.524

## Mean :14.85 Mean :14.56 Mean :0.8710 Mean :5.629

## 3rd Qu.:17.30 3rd Qu.:15.71 3rd Qu.:0.8878 3rd Qu.:5.980

## Max. :21.18 Max. :17.25 Max. :0.9183 Max. :6.675

## width.of.kernel asymmetry.coefficient length.of.kernel.groove

## Min. :2.630 Min. :0.7651 Min. :4.519

## 1st Qu.:2.944 1st Qu.:2.5615 1st Qu.:5.045

## Median :3.237 Median :3.5990 Median :5.223

## Mean :3.259 Mean :3.7002 Mean :5.408

## 3rd Qu.:3.562 3rd Qu.:4.7687 3rd Qu.:5.877

## Max. :4.033 Max. :8.4560 Max. :6.550

## Type

## Canadian:70

## Kama :70

## Rosa :70

##

##

##

(c). What proportion of observations have perimeter values greater than 15?

length(which(seeds$perimeter>15))/length(seeds$perimeter)

## [1] 0.3380952

(d). Which observation has the largest Asymmetry coefficient? To which class does this observa-tion belong?

max(seeds$asymmetry.coefficient)

seeds[which(seeds$asymmetry.coefficient==max(seeds$asymmetry.coefficient)),]

The Canadian kernel type has the largest asymmetry coefficient.

(e). Produce a scatterplot matrix of all variables and note some relationships between them.Which attributes are highly related? Which attributes do a good job of distinguishing type?

pairs(seeds)

2

Page 3: Stat 4510/7510 Homework 2 - University of Missourifaculty.missouri.edu/~schliepe/ewExternalFiles/hw2_solutions-1.pdfbelonging to three di erent varieties (Kama, Rosa and Canadian)

Stat 4510/7510 3/8

Area13

1517

5.0

6.0

26

12 18

1.0

2.0

3.0

13 15 17

perimeter

compactness

0.82 0.90

5.0 6.0

length.of.kernel

width.of.kernel

2.6 3.4

2 6

asymmetry.coefficient

length.of.kernel.groove

4.5 5.5 6.5

1.0 2.0 3.0

1218

0.82

0.90

2.6

3.4

4.5

5.5

6.5

Type

The highly correlated attributes are: area, perimeter, length.of.kernel, width.of.kernel, andlength.of.kernel.groove. Attributes that show distinctions in kernel type include area, perime-ter, length.of.kernel, and width.of.kernel.

(f). Select two or three attributes and produce boxplots of these attributes vs. Type (don’t forgetaxis labels!). What two or three variables might you would want to use as predictor variablesin a model for kernel type based on these plots. Why?

boxplot(seeds$Area~seeds$Type,

xlab="Kernel type",ylab="Kernel area")

3

Page 4: Stat 4510/7510 Homework 2 - University of Missourifaculty.missouri.edu/~schliepe/ewExternalFiles/hw2_solutions-1.pdfbelonging to three di erent varieties (Kama, Rosa and Canadian)

Stat 4510/7510 4/8

Canadian Kama Rosa

1214

1618

20

Kernel type

Ker

nel a

rea

boxplot(seeds$compactness~seeds$Type,

xlab="Kernel type",ylab="Kernel compactness")

4

Page 5: Stat 4510/7510 Homework 2 - University of Missourifaculty.missouri.edu/~schliepe/ewExternalFiles/hw2_solutions-1.pdfbelonging to three di erent varieties (Kama, Rosa and Canadian)

Stat 4510/7510 5/8

Canadian Kama Rosa

0.82

0.84

0.86

0.88

0.90

0.92

Kernel type

Ker

nel c

ompa

ctne

ss

boxplot(seeds$length.of.kernel.groove~seeds$Type,

xlab="Kernel type",ylab="Kernel groove length")

5

Page 6: Stat 4510/7510 Homework 2 - University of Missourifaculty.missouri.edu/~schliepe/ewExternalFiles/hw2_solutions-1.pdfbelonging to three di erent varieties (Kama, Rosa and Canadian)

Stat 4510/7510 6/8

Canadian Kama Rosa

4.5

5.0

5.5

6.0

6.5

Kernel type

Ker

nel g

roov

e le

ngth

I’d suggest area, compactness, and length.of.kernel.groove. Each of these appears less corre-lated with each other and shows from distinction in the kernel type.

(g). Augment your pairs() function from part (e) to give a different color to each type. Hint:col=“green” would make all the points green, so col = [variable] will color them according toa variable. What additional information does this figure give you in building a model forseed type?

pairs(seeds,col=seeds$Type)

6

Page 7: Stat 4510/7510 Homework 2 - University of Missourifaculty.missouri.edu/~schliepe/ewExternalFiles/hw2_solutions-1.pdfbelonging to three di erent varieties (Kama, Rosa and Canadian)

Stat 4510/7510 7/8

Area13

1517

5.0

6.0

26

12 18

1.0

2.0

3.0

13 15 17

perimeter

compactness

0.82 0.90

5.0 6.0

length.of.kernel

width.of.kernel

2.6 3.4

2 6

asymmetry.coefficient

length.of.kernel.groove

4.5 5.5 6.5

1.0 2.0 3.0

1218

0.82

0.90

2.6

3.4

4.5

5.5

6.5

Type

This figure helps to differentiate the different types of kernels for each pair of covariates. Itoffers a nice visualization for determine which covariates might be important in using in amodel.

2. 7510*

One approach for classifying variables (e.g., identifying the type of kernel) uses the k nearestneighbors (KNN). In general, the classifications are based on similarities in covariates (we willlearn more about this approach in future chapters). We will apply the KNN method to the seedsdataset using the knn() function in the class package. In R, a “package” is a collection of functionswhich extend the capabilities of your R installation. There are many packages available and wewill learn and use many throughout this class. To install a specific package, enter the followingcode or click the packages tab in Rstudio and select ‘install.’

7

Page 8: Stat 4510/7510 Homework 2 - University of Missourifaculty.missouri.edu/~schliepe/ewExternalFiles/hw2_solutions-1.pdfbelonging to three di erent varieties (Kama, Rosa and Canadian)

Stat 4510/7510 8/8

install.packages("class")

library(class)

(a). First, randomly split the data into a training and test set. We will “holdout” 30% of thedata randomly, and train our KNN model on the remaining 70%. To accomplish this we canrandomly select rows using the sample() function, then define our test dataset as seeds.testand our training dataset as seeds.train. Be sure to set a seed of 1 for consistent results.

set.seed(1)

TestSamples=sample(1:nrow(seeds),size=.30*nrow(seeds))

seeds.test=seeds[TestSamples,]

seeds.train=seeds[-TestSamples,]

Be sure to make seeds.train which consists of the remaining observations

(b). Predict the classes of the test set using the knn() function using K=10. The knn functionstakes the following arguments:

type.pred=knn(seeds.train[,1:7],seeds.test[,1:7],seeds.train[,8],k=10)

Where train is a matrix of the training attribute variables (no labels!), test is a matrix ofthe test attribute variables, cl is a vector with the class labels in the training set, and k isthe number of nearest neighbors.

(c). Find the misclassification rate of your predictions

length(which(type.pred!=seeds.test[,8]))/length(type.pred)

The misclassification rate is 6.35%.

(d). Investigate the effects of the number of nearest neighbors (k) and the % training vs % testingon misclassification rate. Comment on your results.

The more training data, the lower the misclassification rate for the test data. As for the num-ber of neighbors, the misclassification rate will increase for too few and too many neighbors.

8