Calculate peak height of histogram in R

2.1k views Asked by At

I'm trying to extract some metrics from an histogram in R. For that I have an image in nifti and I plot the correspondent histogram. After that I want to extract mean, median, peak height, peak value and peak width. For that I have the following code:

img = readNIfTI("FA_skeleton_subj_0") #read nifti image
library(HistogramTools)
PlotRelativeFrequency(hist( x = img[ !img==0 ], xlim=c(0,0.8), breaks = seq(0,0.7,0.001)), xlab = "FA", main = "Histogram de FA") #plot relative frequency so I can compare between subjects
mean (img[ !img==0 ]) #!img==0 means that I don't want to count with zero voxels because they are background
median(img[ !img==0])
abline(v=median(img[ !img==0]),col="green")
abline(v=mean(img[ !img==0]),col="blue")

I would appreaciate some help to calculate the peak metrics (height, width and value). Thanks!

1

There are 1 answers

3
Vincent Guillemot On

Here is an example on a random Gaussian sample:

x <- rnorm(1000)
h <- hist(x, n=20)
i <- which.max(h$density)
res <- c("Peak value" = mean(h$breaks[i:(i+1)]), 
         "Peak height" = h$density[i], 
         "Peak width" = diff(h$breaks[i:(i+1)]))

Additionally, you may want to compute the mode of the sample, which can be done with one of the functions of the modeest package:

library(modeest)
m <- asselin(x)