0

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!

anaF
  • 1
  • 1
  • Can you provide a reproducible example? What is `img`, just a matrix? The only thing that can't be found straightforward from raw data is peak width. – Roman Luštrik Sep 06 '17 at 09:34
  • img is an MRI image in nifti format – anaF Sep 06 '17 at 09:43
  • I would share a concrete example, but the comment area is not accepting the sharing link – anaF Sep 06 '17 at 10:57
  • See https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example on how to easily make an example which you can share. – Roman Luštrik Sep 06 '17 at 11:48

1 Answers1

0

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)
Vincent Guillemot
  • 3,394
  • 14
  • 21