EMCluster package prior to use
FClust.fdapace.library(fdapace)
library(dplyr)
library(ggplot2)
library(gridExtra)
library(scales)
data("medfly25")
Flies <- MakeFPCAInputs(medfly25$ID,medfly25$Days,medfly25$nEggs)
Chen and Maitra (2015) Chiou and Li (2007)
newClust <- FClust(
Flies$Ly,Flies$Lt,k=2,
optnsFPCA = list(
methodMuCovEst="smooth",
userBwCov=2,
FVEthreshold=0.90
)
)
After observing data closely, we found that clusters are differentiated by the number of eggs that could be laid. The threshold value may be 25.
veryLowCount = ifelse(
sapply(unique(medfly25$ID), function(u)
sum(medfly25$nEggs[medfly25$ID==u]))>=25,1,2)
veryLowCount
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[48] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[95] 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 2 1 1 1 1 2 1 1 1 2 2 1 1 1 1 1 1 1 1
[142] 1 2 1 1 2 2 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 2 1 1 1 1 1 1 1 1 1 1 1 2 2 2 1 1
[189] 1 1 1 2 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1
[236] 1 1 1 1 1 2 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[283] 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[330] 1 1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Clust$cluster==1,]
c2flies <- medfly25[newClust$cluster==2,]
c1fliesObj <- MakeFPCAInputs(c1flies$ID,c1flies$Days,c1flies$nEggs)
c2fliesObj <- MakeFPCAInputs(c2flies$ID,c2flies$Days,c2flies$nEggs)
c1fliesFpca <- FPCA(c1fliesObj$Ly,c1fliesObj$Lt,list(methodMuCovEst="smooth"))
plot(c1fliesFpca)
c2fliesFpca <- FPCA(c2fliesObj$Ly,c2fliesObj$Lt,list(methodMuCovEst="smooth"))
plot(c2fliesFpca)
par(mfrow=c(1,2))
CreateModeOfVarPlot(c1fliesFpca,main="Cluster 1")
CreateModeOfVarPlot(c2fliesFpca,main="Cluster 2")
par(mfrow=c(1,1))