我正在使用以下法典,在R创造标准正常分配:
x <- seq(-4, 4, length=200)
y <- dnorm(x, mean=0, sd=1)
plot(x, y, type="l", lwd=2)
我需要将X-轴贴上上面和以下三个标准偏离点的标签。 我如何添加这些标签?
我正在使用以下法典,在R创造标准正常分配:
x <- seq(-4, 4, length=200)
y <- dnorm(x, mean=0, sd=1)
plot(x, y, type="l", lwd=2)
我需要将X-轴贴上上面和以下三个标准偏离点的标签。 我如何添加这些标签?
最容易(但并非一般)的方式是限制轴心的极限。 +/-1:3 sigma将贴上标签,平均标记为0-表示偏离平均值。
plot(x,y, type = "l", lwd = 2, xlim = c(-3.5,3.5))
Another option is to use more specific labels:
plot(x,y, type = "l", lwd = 2, axes = FALSE, xlab = "", ylab = "")
axis(1, at = -3:3, labels = c("-3s", "-2s", "-1s", "mean", "1s", "2s", "3s"))
利用https://stackoverflow.com/a/10489547/3924118”中的代码,请在x
和仅使用 curve(
上填写<>dnorm:
curve(dnorm, -3.5, 3.5, lwd=2, axes = FALSE, xlab = "", ylab = "")
axis(1, at = -3:3, labels = c("-3s", "-2s", "-1s", "mean", "1s", "2s", "3s"))
但是,这更没有使用特定的法典。
例如,一般情况下:正常(2)1
f <- function(x) dnorm(x, 2, 1)
plot(f, -1, 5)
这是非常笼统的, f 可自由界定,并包含任何特定参数,例如:
f <- function(x) dbeta(x, 0.1, 0.1)
plot(f, 0, 1)
在蒙特卡洛模拟思想基础上开展工作的一个极端低效和异常但美丽的解决办法是:
rnorm
. The rnorm
function takes as arguments (A,B,C) and returns a vector of A samples from a normal distribution centered at B, with standard deviation C. 因此,从标准正常(即平均0和标准偏离1的正常状态)中抽出50 000人的面积,并划定其密度。
x = rnorm(50000,0,1)
plot(density(x))
随着人数的推移,这一数目将趋向于向正常分配。 为了说明这一点,见以下图象:从左到右上到底,5000 500 000和500万个样本:获得500万个样本。
I particularly love Lattice for this goal. It easily implements graphical information such as specific areas under a curve, the one you usually require when dealing with probabilities problems such as find P(a < X < b) etc. Please have a look:
library(lattice)
e4a <- seq(-4, 4, length = 10000) # Data to set up out normal
e4b <- dnorm(e4a, 0, 1)
xyplot(e4b ~ e4a, # Lattice xyplot
type = "l",
main = "Plot 2",
panel = function(x,y, ...){
panel.xyplot(x,y, ...)
panel.abline( v = c(0, 1, 1.5), lty = 2) #set z and lines
xx <- c(1, x[x>=1 & x<=1.5], 1.5) #Color area
yy <- c(0, y[x>=1 & x<=1.5], 0)
panel.polygon(xx,yy, ..., col= red )
})
在此例子中,我把<代码>z = 1和z = 1.5/code>之间的区域划出。 你可以根据你的问题轻而易举地改变这一参数。
轴标签是自动的。
这是如何在职能中写明:
normalCriticalTest <- function(mu, s) {
x <- seq(-4, 4, length=200) # x extends from -4 to 4
y <- (1/(s * sqrt(2*pi))) * exp(-((x-mu)^2)/(2*s^2)) # y follows the formula
of the normal distribution: f(Y)
plot(x,y, type="l", lwd=2, xlim = c(-3.5,3.5))
abline(v = c(-1.96, 1.96), col="red") # draw the graph, with 2.5% surface to
either side of the mean
}
normalCriticalTest(0, 1) # draw a normal distribution with vertical lines.
最后结果:
This is a really really simple question to which I seem to be entirely unable to get a solution. I would like to do a scatter plot of an observed time series in R, and over this I want to plot the ...
since the latest Ubuntu release (karmic koala), I noticed that the internal R package advertises on start-up the REvolution package. It seems to be a library collection for high-performance matrix ...
I am trying to run an analysis by invoking R through the command line as follows: R --no-save < SampleProgram.R > SampleProgram.opt For example, consider the simple R program below: mydata =...
I m trying to generate GOFrame objects to generate a gene ontology mapping in R for unsupported organisms (see http://www.bioconductor.org/packages/release/bioc/vignettes/GOstats/inst/doc/...
I have a dataframe df.all and I m plotting it in a bar plot with ggplot2 using the code below. I d like to make it so that the order of the dodged bars is flipped. That is, so that the bars labeled "...
I m getting a weird error when training a glmnet regression. invalid class "dgCMatrix" object: length(Dimnames[[2]]) must match Dim[2] It only happens occasionally, and perhaps only under larger ...
Sorry for the non-descriptive title but I don t know whether there s a word for what I m trying to achieve. Let s assume that I have a list of names of different classes like c( 1 , 2 , 3 , 4 ) ...
I m plotting a group of curves, using facet in ggplot2. I d like to have a smoother applied to plots where there are enough points to smooth, but not on plots with very few points. In particular I d ...