Use the data mtcars in R:
1. Describe the data in R. (e.g., how many variables, types of variables).
2. Clean the data by deleting missing values.
3. Normalize the data/standardize the variables.
4. Determine the number of clusters.
5. Use k means cluster analysis.
6. Get cluster means.
7. Visualize the clustering result.
Solution-A:
Rcode:
dim(mtcars)
colnames(mtcars)
there were 32 observations and 11 columns.they are
mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
"carb"
Solution-B
mydata <- na.omit(mtcars) # listwise deletion of
missing
mydata <- scale(mydata)
3. Normalize the data/standardize the variables.
mydata <- scale(mydata)
mydata
4. Determine the number of clusters.
dim(mydata)
wss <- (nrow(mydata)-1)*sum(apply(mydata,2,var))
for (i in 2:15) wss[i] <- sum(kmeans(mydata,
centers=i)$withinss)
plot(1:15, wss, type="b", xlab="Number of Clusters",
ylab="Within groups sum of squares")
#k=9 clusters
K-Means Cluster Analysis
fit <- kmeans(mydata, 9)
Get Answers For Free
Most questions answered within 1 hours.