我试图从R中的直方图中提取一些指标。为此,我有一个nifti的图像,我绘制了相应的直方图。之后,我想提取平均值,中位数,峰高,峰值和峰宽。为此,我有以下代码:
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")
我会提供一些帮助来计算峰值指标(高度,宽度和值)。谢谢!
答案 0 :(得分:0)
以下是随机高斯样本的示例:
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)]))
此外,您可能想要计算样本的模式,这可以使用modeest
包的一个函数来完成:
library(modeest)
m <- asselin(x)