R作图 Binomial Distribution(±)
二项分布和负二项分布,图示如下:
(1)二项分布Rcode
> N <- 100000
> n <- 100
> p <- 0.9
> x <- rbinom(N, n, p)
> hist(x,
+ xlim = c(min(x), max(x)),
+ probability = T, #概率函数,默认为频率函数
+ nclass = max(x) - min(x) +1,
+ col = 'lightblue',
+ main = "Binomial Distribution, n=100, p=.9" )
> lines(density(x,
+ bw=1, #控制函数的峰度??
+ kernel = 'gaussian'), #默认的函数拟合是高斯函数
+ col = 'blue',
+ lwd = 3) #可以控制线宽
(2)负二项分布Rcode
> N <- 100000
> n <- 100
> p <- 0.9
> x <- rnbinom(N, n, p)
> hist(x,
+ xlim = c(min(x), max(x)),
+ probability = T,
+ nclass = max(x) - min(x) +1,
+ col = 'lightblue',
+ main = "Negative Binomial Distribution, n=100, p=.9" )
> lines(density(x,
+ bw=1,
+ kernel = 'gaussian'),
+ col = 'blue',
+ lwd = 3)
----------------------------------------------------------------------------------------------------------
函数简单解释:
(1) Gaussian function
In mathematics, a Gaussian function is a function of the form:
for some real constants a, b , c, and e ≈ 2.718281828(Euler's number)
(http://en.wikipedia.org/wiki/Gaussian_function)
(2) Histograms
Description
The generic function hist
computes a histogram of the given data values. If plot=TRUE
, the resulting object of class "histogram"
is plotted by plot.histogram
, before it is returned.
(http://127.0.0.1:12956/library/graphics/html/hist.html)
Usage
hist(x, ...)
## Default S3 method:
hist(x, breaks = "Sturges",
freq = NULL, probability = !freq,
include.lowest = TRUE, right = TRUE,
density = NULL, angle = 45, col = NULL, border = NULL,
main = paste("Histogram of" , xname),
xlim = range(breaks), ylim = NULL,
xlab = xname, ylab,
axes = TRUE, plot = TRUE, labels = FALSE,
nclass = NULL, warn.unused = TRUE, ...)
Arguments
x |
a vector of values for which the histogram is desired. |
breaks |
one of:
In the last three cases the number is a suggestion only. |
freq |
logical; if |
probability |
an alias for |
include.lowest |
logical; if |
right |
logical; if |
density |
the density of shading lines, in lines per inch. The default value of |
angle |
the slope of shading lines, given as an angle in degrees (counter-clockwise). |
col |
a colour to be used to fill the bars. The default of |
border |
the color of the border around the bars. The default is to use the standard foreground color. |
main, xlab, ylab |
these arguments to |
xlim, ylim |
the range of x and y values with sensible defaults. Note that |
axes |
logical. If |
plot |
logical. If |
labels |
logical or character. Additionally draw labels on top of bars, if not |
nclass |
numeric (integer). For S(-PLUS) compatibility only, |
warn.unused |
logical. If |
... |
further arguments and graphical parameters passed to |
(3)Kernel Density Estimation
Description
The (S3) generic function density
computes kernel density estimates. Its default method does so with the given kernel and bandwidth for univariate observations.
(http://127.0.0.1:12956/library/stats/html/density.html)
Usage
density(x, ...)
## Default S3 method:
density(x, bw = "nrd0", adjust = 1,
kernel = c("gaussian", "epanechnikov", "rectangular",
"triangular", "biweight",
"cosine", "optcosine"),
weights = NULL, window = kernel, width,
give.Rkern = FALSE,
n = 512, from, to, cut = 3, na.rm = FALSE, ...)
Arguments
x |
the data from which the estimate is to be computed. |
bw |
the smoothing bandwidth to be used. The kernels are scaled such that this is the standard deviation of the smoothing kernel. (Note this differs from the reference books cited below, and from S-PLUS.)
The specified (or computed) value of |
adjust |
the bandwidth used is actually |
kernel, window |
a character string giving the smoothing kernel to be used. This must be one of
|
weights |
numeric vector of non-negative observation weights, hence of same length as |
width |
this exists for compatibility with S; if given, and |
give.Rkern |
logical; if true, no density is estimated, and the ‘canonical bandwidth’ of the chosen |
n |
the number of equally spaced points at which the density is to be estimated. When |
from,to |
the left and right-most points of the grid at which the density is to be estimated; the defaults are |
cut |
by default, the values of |
na.rm |
logical; if |
... |
further arguments for (non-default) methods. |