Question: l am trying to run this code in R but am receiving errors with the ClassProp and clusplot functions. I have tried researching online, but

l am trying to run this code in R but am receiving errors with the ClassProp and clusplot functions. I have tried researching online, but I am a new R user and do not understand what I am doing wrong or what should change in order to make the lengths of partitioning vectors match and have the correct length. Can someone please explain and show what the updated code should be for it to run correctly?
Code:
library("MixSim")
library("MASS")
library("cluster")
library("mclust")
data(iris)
#1.Create a true.id variable based on Species variable from the data.
id.true <- as.factor(iris$Species)
#2.Form a data set based on the first 4 columns, i.e., without the Species variable.
X <- iris[,1:4]
#a.Hierarchical Clustering with Single Linkage
d <- dist(X)
H.single <- hclust(d, method = "single")
id.single <- cutree(H.single, 4)
#Calculating ACCURACY
ClassProp(id.true, id.single)
#b.Hierarchical Clustering with Complete Linkage
d <- dist(X)
H.complete <- hclust(d, method = "complete")
id.complete <- cutree(H.complete, 4)
# Calculating ACCURACY
ClassProp(id.true, id.complete)
#c.K-means
d <- dist(X)
id.true <- rep(1:4, each =50)
id.kmeans <- kmeans(X,4)$clust
#Calculating ACCURACY
ClassProp(id.true, id.kmeans)
kmeans_model <- kmeans(X, centers =3, nstart =20)
#d.Model-based clustering from package mclust
mclust_model <- Mclust(X, G =3)$class
#4.Create a plot using function clusplot() from package cluster.
clusplot(X, H.single, lines =0, color = TRUE, plotchar = FALSE, main = "Hierarchical (Single Linkage)")
clusplot(X, H.complete, lines =0, color = TRUE, plotchar = FALSE, main = "Hierarchical (Complete Linkage)")
clusplot(X, kmeans_model, lines =0, color = TRUE, plotchar = FALSE, main ="K-means")
clusplot(X, mclust_model, lines =0, color = TRUE, plotchar = FALSE, main = "Model-based Clustering")
Errors:
> ClassProp(id.true, id.single)
Error in ClassProp(id.true, id.single) :
NAs in foreign function call (arg 4)
In addition: Warning messages:
1: In `[<-.factor`(`*tmp*`, ind, value =0) :
invalid factor level, NA generated
2: In `[<-.factor`(`*tmp*`, ind, value =1) :
invalid factor level, NA generated
3: In `[<-.factor`(`*tmp*`, ind, value =2) :
invalid> ClassProp(id.true, id.complete)
Error in ClassProp(id.true, id.complete) :
NAs in foreign function call (arg 4)
In addition: Warning messages:
1: In `[<-.factor`(`*tmp*`, ind, value =0) :
invalid factor level, NA generated
2: In `[<-.factor`(`*tmp*`, ind, value =1) :
invalid factor level, NA generated
3: In `[<-.factor`(`*tmp*`, ind, value =2) :
invalid factor level, NA generated factor level, NA generated
> ClassProp(id.true, id.complete)
Error in ClassProp(id.true, id.complete) :
NAs in foreign function call (arg 4)
In addition: Warning messages:
1: In `[<-.factor`(`*tmp*`, ind, value =0) :
invalid factor level, NA generated
2: In `[<-.factor`(`*tmp*`, ind, value =1) :
invalid factor level, NA generated
3: In `[<-.factor`(`*tmp*`, ind, value=2) :
invalid factor level, NA generated
> ClassProp(id.true, id.kmeans)
Error in ClassProp(id.true, id.kmeans) :
Lengths of partitioning vectors do not match...
> #4.Create a plot using function clusplot() from package cluster.
> clusplot(X, H.single, lines =0, color = TRUE, plotchar = FALSE, main = "Hierarchical (Single Linkage)")
Error in clusplot.default(X, H.single, lines =0, color = TRUE, plotchar = FALSE, :
The clustering vector is of incorrect length
> clusplot(X, H.complete, lines =0, color = TRUE, plotchar = FALSE, main = "Hierarchical (Complete Linkage)")
Error in clusplot.default(X, H.complete, lines =0, color = TRUE, plotchar = FALSE, :
The clustering vector is of incorrect length
> clusplot(X, kmeans_model, lines =0, color = TRUE, plotchar = FALSE, main ="K-means")
Error in clusplot.default(X, kmeans_model, lines =0, color = TRUE, plotchar = FALSE, :
The clustering vector is of incorrect length

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!