制作概率密度和相关中断的表/矩阵

时间:2016-05-09 05:44:48

标签: r matrix histogram bootstrapping probability-density

以下代码采用向量V1,并引导10000次由V1构成的随机化rnomal样本,并在结果中创建一个10000列的矩阵。然后它为该矩阵创建直方图。

V1 <- c(0.18, 0.2, 0.24, 0.35, -0.22, -0.17, 0.28, -0.28, -0.14, 0.03, 0.87, -0.2, 0.06, -0.1, -0.72, 0.18, 0.01, 0.31, -0.36, 0.61, -0.16, -0.07, -0.13, 0.01, -0.09, 0.26, -0.14, 0.08, -0.62, -0.2, 0.3, -0.21, -0.11, 0.05, 0.06, -0.28, -0.27, 0.17, 0.42, -0.05, -0.15, 0.05, -0.07, -0.22, -0.34, 0.16, 0.34, 0.1, -0.12, 0.24, 0.45, 0.37, 0.61, 0.9, -0.25, 0.02)

xxx <- round(sapply(1:10000, function(i) rnorm(length(V1), mean=sample
                                      (V1, length(V1), replace=TRUE), sd(V1))),2)

h <- hist(xxx, plot=T)

我想以矩阵或表格格式创建其概率密度函数的打印输出,即获得1.0,0.9,0.8,0.7,0.6,0.5,0.4,0.3,0.2,0.1,0,-0的矩阵,1 -0.2,-0.3,-0.4,-0.5,-0.6,-0.7,-0.8,-0.9,-1.0作为中断,以及下一列的相关概率密度。

我的问题是两个。首先,指定我想要的休息失败。其次,使用h$breaksh$density制作矩阵也会失败。任何见解都将非常感激。谢谢。

#This works, but I want to specify the breaks
h <- hist(xxx, plot=T, breaks=20)

#This gives error "some 'x' not counted; maybe 'breaks' do not span range of 'x'"
h <- hist(xxx, plot=T, breaks=c(1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0, -0,1 -0.2, -0.3, -0.4, -0.5, -0.6, -0.7, -0.8, -0.9, -1.0))

#This gives error number of rows of result is not a multiple of vector length
ddd<- cbind(h$breaks, h$density)

1 个答案:

答案 0 :(得分:1)

首先你有一个类型错误,你混淆了“。”和“,”。

第二次,休息时间必须涵盖整个观察范围:

h <- hist(xxx, plot=F, breaks=c(max(xxx),1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0, -0.1, -0.2, -0.3, -0.4, -0.5, -0.6, -0.7, -0.8, -0.9, -1.0,min(xxx)))

最后,你想选择“mids”而不是休息:

ddd<- cbind(h$mids, h$density)