Sorting ordinal Data

Sorry if I'm missing the point and adding to the confusion, but it seems to me rather than literally ordering the factors you're trying to group them to compare each individual soldier to all soldiers. So in that case, hierarchical clustering might help you find the groups of soldiers that have most similar ossification (and optionally the groups of bones that reveal some pattern). And if you find nice groups, then you can take the mean per group which is more meaningful than the mean across all bones.

There are many ways to perform such a clustering, you could try the package pheatmap with default options. If you try to go with that option you'll need to get rid of the NAs. For this purpose it makes sense to replace them with the average on the bone with code like:

DATA_no_na <- apply(DATA[,-1], 2,
                    function(xx) round(ifelse(is.na(xx), mean(xx, na.rm = TRUE), xx)))
rownames(DATA_no_na) <- DATA[,1]
pheatmap::pheatmap(DATA_no_na)
1 Like

That sounds logical, I will look into these packages. But again calculating means is not correct for ordinal data.

#made up example data
set.seed(1)
vals <- matrix(as.integer(rnorm(n=100,mean = 2 , sd =1 )),nrow = 10,ncol=10)

fl <- c(0,1,2,3)

library(tidyverse)
df1 <- as.data.frame(vals) %>% mutate_all(~if_else(.==0,NA_integer_,.)) %>% 
        mutate_all(~factor(.,levels=c(1,2,3))) %>% mutate(id = row_number()) %>% select(id,everything())

#begin here, summarise the info
 df_long <- pivot_longer(df1,
                         cols = -id)
 
 df_gsum <- group_by(df_long,
                     id) %>% summarise(
                             c1 = sum(value==1,na.rm = TRUE),
                             c2 = sum(value==2,na.rm = TRUE),
                             c3 = sum(value==3,na.rm = TRUE)
                     ) %>% arrange(c1,c2,c3)

something like this where you count how many bones are at a given level. so each sample is represented by 3 numbers, instead of 42 numbers.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.