84 lines
2.5 KiB
R
84 lines
2.5 KiB
R
|
|
## Not run:
|
||
|
|
# import and format a mixed-type data set
|
||
|
|
library(kamila)
|
||
|
|
data(Byar, package='clustMD')
|
||
|
|
|
||
|
|
ds <- readr::read_csv("2 Longterm/assigndata.csv",na = c("","NA")) |> na.omit()
|
||
|
|
|
||
|
|
ds <- ds |> dplyr::mutate(mrs_1 = mrs_1>2)
|
||
|
|
|
||
|
|
cat_i <- lapply(ds,is.character) |> purrr::list_c()
|
||
|
|
con_i <- lapply(ds,is.double) |> purrr::list_c()
|
||
|
|
|
||
|
|
xor(cat_i,con_i)
|
||
|
|
|
||
|
|
clin_clust=2
|
||
|
|
# Byar$logSpap <- log(Byar$Serum.prostatic.acid.phosphatase)
|
||
|
|
|
||
|
|
# conInd <- c(5,6,8:10,16)
|
||
|
|
# conVars <- Byar[,conInd]
|
||
|
|
# conVars <- data.frame(scale(conVars))
|
||
|
|
|
||
|
|
conVars <- ds[,con_i]
|
||
|
|
conVars <- data.frame(scale(conVars))
|
||
|
|
|
||
|
|
# catVarsFac <- Byar[,-c(1:2,conInd,11,14,15)]
|
||
|
|
# catVarsFac[] <- lapply(catVarsFac, factor)
|
||
|
|
# catVarsDum <- dummyCodeFactorDf(catVarsFac)
|
||
|
|
|
||
|
|
catVarsFac <- ds[,cat_i]
|
||
|
|
catVarsFac <- lapply(catVarsFac, factor) |> dplyr::bind_cols() |> as.data.frame()
|
||
|
|
catVarsDum <- dummyCodeFactorDf(catVarsFac)
|
||
|
|
|
||
|
|
# Modha-Spangler clustering with kmeans default Hartigan-Wong algorithm
|
||
|
|
gmsResHw <- gmsClust(conVars, catVarsDum, nclust = clin_clust)
|
||
|
|
|
||
|
|
# Modha-Spangler clustering with kmeans Forgy-Lloyd algorithm
|
||
|
|
# NOTE searchDensity should be >= 10 for optimal performance:
|
||
|
|
# this is just a syntax demo
|
||
|
|
gmsResLloyd <- gmsClust(conVars, catVarsDum, nclust = clin_clust,
|
||
|
|
algorithm = "Lloyd", searchDensity = 15)
|
||
|
|
|
||
|
|
# KAMILA clustering
|
||
|
|
kamRes <- kamila(conVars, catVarsFac, numClust=2:7, numInit=10, calcNumClust="ps")
|
||
|
|
|
||
|
|
# Plot results
|
||
|
|
# ternarySurvival <- factor(Byar$SurvStat)
|
||
|
|
# levels(ternarySurvival) <- c('Alive','DeadProst','DeadOther')[c(1,2,rep(3,8))]
|
||
|
|
plottingData <- cbind(
|
||
|
|
conVars,
|
||
|
|
catVarsFac,
|
||
|
|
KamilaCluster = factor(kamRes$finalMemb))
|
||
|
|
# plottingData$Bone.metastases <- ifelse(
|
||
|
|
# plottingData$Bone.metastases == '1', yes='Yes',no='No')
|
||
|
|
#
|
||
|
|
# # Plot Modha-Spangler/Hartigan-Wong results
|
||
|
|
# msPlot <- ggplot(
|
||
|
|
# plottingData,
|
||
|
|
# aes(
|
||
|
|
# x=logSpap,
|
||
|
|
# y=Index.of.tumour.stage.and.histolic.grade,
|
||
|
|
# color=ternarySurvival,
|
||
|
|
# shape=MSCluster))
|
||
|
|
# plotOpts <- function(pl) (pl + geom_point() +
|
||
|
|
# scale_shape_manual(values=c(2,3,7)) + geom_jitter())
|
||
|
|
# plotOpts(msPlot)
|
||
|
|
|
||
|
|
# Plot KAMILA results
|
||
|
|
kamPlot <- ggplot(
|
||
|
|
plottingData,
|
||
|
|
aes(
|
||
|
|
x=pase_0,
|
||
|
|
y=pase_6,
|
||
|
|
color=KamilaCluster,
|
||
|
|
shape=KamilaCluster))
|
||
|
|
plotOpts(kamPlot)
|
||
|
|
|
||
|
|
|
||
|
|
plotting_ls <- tibble(KamilaCluster = factor(kamRes$finalMemb),
|
||
|
|
MSCluster = factor(gmsResHw$results$cluster)) |>
|
||
|
|
purrr::map(\(x) cbind(x, ds))
|
||
|
|
|
||
|
|
plotting_ls |> purrr::map(\(y) {
|
||
|
|
y |> gtsummary::tbl_summary(by=x) |> gtsummary::add_p() |> gtsummary::add_overall()}) |>
|
||
|
|
gtsummary::tbl_merge()
|