7

ビン内の要素数によってビンが正規化されているヒストグラムをプロットしようとしています。

私は以下を使用しています

binwidth=5
bin(x,width)=width*floor(x/width) + binwidth/2.0
plot 'file' using (bin($2, binwidth)):($4) smooth freq with boxes

基本的なヒストグラムを取得しますが、各ビンの値をビンのサイズで割りたいと思います。gnuplot で、または必要に応じて外部ツールを使用してこれを行うにはどうすればよいですか?

4

5 に答える 5

9

gnuplot 4.4 では、関数は複数の連続したコマンドを実行して値を返すことができるという点で、異なる特性を持ちます ( gnuplot のトリックを参照)。事前に知ること。このコードは、1 つの列 (正規分布からの n 個のサンプルのリスト) を含むファイル「out.dat」に対して実行されます。

binwidth = 0.1
set boxwidth binwidth
sum = 0

s(x)          = ((sum=sum+1), 0)
bin(x, width) = width*floor(x/width) + binwidth/2.0

plot "out.dat" u ($1):(s($1))
plot "out.dat" u (bin($1, binwidth)):(1.0/(binwidth*sum)) smooth freq w boxes

最初の plot ステートメントは、データファイルを読み取り、点ごとに sum を 1 回インクリメントして、ゼロをプロットします。

2 番目の plot ステートメントは、実際には sum の値を使用してヒストグラムを正規化します。

于 2011-12-21T14:54:54.847 に答える
8

statsgnuplot 4.6 では、コマンドで点数を数えることができます。これは よりも高速ですplot。実際には、そのようなトリックは必要ありませんが、 を実行した後s(x)=((sum=sum+1),0)、直接変数で数をカウントします。STATS_recordsstats 'out.dat' u 1

于 2012-10-24T06:29:00.700 に答える
4

次のコマンドを使用してRから生成されたn=500のランダムなガウス変量を使用して、次のようにします。

Rscript -e 'cat(rnorm(500), sep="\\n")' > rnd.dat

正規化されたヒストグラムを定義するために、あなたとまったく同じアイデアを使用します。ここで、yは1 /(binwidth * n)として定義されますが、int代わりに使用floorし、bin値で最近使用しなかった点が異なります。要するに、これはsmooth.demデモスクリプトからの迅速な適応であり、同様のアプローチがJanertの教科書Gnuplot in Action第13章、257ページ、無料で入手可能)に記載されています。サンプルデータファイルは、Gnuplotに付属random-pointsのフォルダにあるものに置き換えることができます。demoファイル内のレコードのカウント機能がないため、ポイント数をGnuplotとして指定する必要があることに注意してください。

bw1=0.1
bw2=0.3
n=500
bin(x,width)=width*int(x/width)
set xrange [-3:3]
set yrange [0:1]
tstr(n)=sprintf("Binwidth = %1.1f\n", n) 
set multiplot layout 1,2
set boxwidth bw1
plot 'rnd.dat' using (bin($1,bw1)):(1./(bw1*n)) smooth frequency with boxes t tstr(bw1)
set boxwidth bw2
plot 'rnd.dat' using (bin($1,bw2)):(1./(bw2*n)) smooth frequency with boxes t tstr(bw2)

これが2つのビン幅の結果です

ここに画像の説明を入力してください

さらに、これは実際にはヒストグラムへの大まかなアプローチであり、Rではより詳細なソリューションをすぐに利用できます。実際、問題は適切なビン幅を定義する方法であり、この問題はstats.stackexchange.comですでに説明されています:Freedman-の使用四分位範囲を計算する必要がありますが、ダイアコニスビニングルールの実装はそれほど難しくありません。

これは、Rが同じデータセットをどのように処理するかを示しています。デフォルトのオプション(この特定のケースでは違いがないため、Sturgesルール)と上記で使用したような等間隔のビンを使用します。

ここに画像の説明を入力してください

使用されたRコードを以下に示します。

par(mfrow=c(1,2), las=1)
hist(rnd, main="Sturges", xlab="", ylab="", prob=TRUE)
hist(rnd, breaks=seq(-3.5,3.5,by=.1), main="Binwidth = 0.1", 
     xlab="", ylab="", prob=TRUE)

呼び出し時に返される値を調べることで、Rがどのように機能するかを確認することもできますhist()

> str(hist(rnd, plot=FALSE))
List of 7
 $ breaks     : num [1:14] -3.5 -3 -2.5 -2 -1.5 -1 -0.5 0 0.5 1 ...
 $ counts     : int [1:13] 1 1 12 20 49 79 108 87 71 43 ...
 $ intensities: num [1:13] 0.004 0.004 0.048 0.08 0.196 0.316 0.432 0.348 0.284 0.172 ...
 $ density    : num [1:13] 0.004 0.004 0.048 0.08 0.196 0.316 0.432 0.348 0.284 0.172 ...
 $ mids       : num [1:13] -3.25 -2.75 -2.25 -1.75 -1.25 -0.75 -0.25 0.25 0.75 1.25 ...
 $ xname      : chr "rnd"
 $ equidist   : logi TRUE
 - attr(*, "class")= chr "histogram"

必要に応じて、Rの結果を使用してGnuplotでデータを処理できると言っても過言ではありません(ただし、Rを直接使用することをお勧めします:-)。

于 2011-05-10T10:19:21.557 に答える
2

ファイル内のデータ ポイントの数をカウントするもう 1 つの方法は、システム コマンドを使用することです。これは、複数のファイルをプロットしていて、点の数が事前にわからない場合に役立ちます。私が使用した:

countpoints(file) = system( sprintf("grep -v '^#' %s| wc -l", file) )
file1count = countpoints (file1)
file2count = countpoints (file2)
file3count = countpoints (file3)
...

このcountpoints関数は、「#」で始まる行をカウントしません。次に、前述の関数を使用して、正規化されたヒストグラムをプロットします。

完全な例を次に示します。

n=100
xmin=-50.
xmax=50.
binwidth=(xmax-xmin)/n

bin(x,width)=width*floor(x/width)+width/2.0
countpoints(file) = system( sprintf("grep -v '^#' %s| wc -l", file) )

file1count = countpoints (file1)
file2count = countpoints (file2)
file3count = countpoints (file3)

plot file1 using (bin(($1),binwidth)):(1.0/(binwidth*file1count)) smooth freq with boxes,\
     file2 using (bin(($1),binwidth)):(1.0/(binwidth*file2count)) smooth freq with boxes,\
     file3 using (bin(($1),binwidth)):(1.0/(binwidth*file3count)) smooth freq with boxes
...
于 2013-05-01T16:50:54.787 に答える
-2

単に

plot 'file' using (bin($2, binwidth)):($4/$4) smooth freq with boxes
于 2016-12-15T12:10:50.763 に答える