2016-05-09 40 views
1

以下代碼採用向量V1,並從V1創建的隨機化樣本自舉10000次,並使用結果創建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

起初,你有一個類型的錯誤,你糊塗「」和「」。

在第二個休息時間必須跨越這樣的整個範圍觀察:

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))) 

,最後,你要挑「中音」,而不是休息:

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