This question already has answers here:
setting upper and lower limits in rnorm
(6 answers)
Closed 9 years ago.
I am using rnorm but the outputs I receive are sometimes negative. How do I create a restriction so that the outputs cannot be below 0? Example:
output = rnorm(1, 800/20, sqrt(800))
Why not abs(rnorm(1, 800/20, sqrt(800))? rnorm was written to give numbers from a normal distribution. Perhaps you are looking to get output from a truncated distribution. In that case, you might want to have a look at the truncnorm package.
library(truncnorm)
rtruncnorm( 1, a=0, b=Inf, 800/20, sqrt(800))
x = seq(-20,200,by=0.01)
y = dtruncnorm(x, a=0, b=Inf, 800/20, sqrt(800) )
plot(x,y,type="l",main="Density of a truncated normal distribution")
The Poisson distribution takes only positive values. Otherwise golbasche solution seems perfect.
hist(rpois(100, 5))
Related
This question already has answers here:
Generate matrix with iid normal random variables using R
(4 answers)
Closed 1 year ago.
I'm having trouble figuring out how to perform the following:
Generate a plot for a normally distributed random variable X with a mean of 250 and variance of 625 (SD 25).
Generate a random sample (n=15) from a normally distributed variable Z with mean=10 variance = 400. Using this sample estimate the population mean for Z and 95% confidence interval.
Essentially the main part I'm struggling with is generating a random sample/variable. Thanks!
The function rnorm(n=15,mean=10,sd=sqrt(400)) will supply you with the wanted numbers...
The rnorm() function draws random samples from a normal distribution.
From the normal distribution page in the R manual:
rnorm(n, mean = 0, sd = 1)
n: number of observations. If length(n) > 1, the
length is taken to be the number required.
mean: vector of means.
sd: vector of standard deviations.
So if you need 15 draws with mean 250 and sd 25, rnorm(15, 250, 25).
[1] 250.0760 251.0984 201.1045 231.8379 213.2640 263.3968 274.8070 225.1520
[9] 260.0468 275.5306 295.3408 241.8458 229.2726 285.6786 232.1860
This question already has answers here:
Generate random numbers with fixed mean and sd
(3 answers)
Closed 3 years ago.
What can I do to compel R to give me a sample less than 20, such that the sample will have mean = 0 and variance = var.
MWE
rnorm(20, mean=0, sd=1)
As the sample size gets larger, the sample gets closer to normal. How then can I make R to give me a sample of n < 20 with a mean equal to zero and variance equal to whatever I specify?
Try
rnorm(20, mean=0, sd=sqrt(var))
For what it's worth
your last paragraph sounds like a slightly confused statement of the Central Limit Theorem; there's no difficulty in creating small data sets that are normal, or Gaussian (the CLT says that sums of N independent, identically distributed variables approach normality as N goes to infinity ...)
it would be worth using v rather than var to denote your variance, since var() is a built-in function in R (this is mostly harmless, but occasionally confusing).
I am trying to create a discrete normal distribution using something such as
x <- rnorm(1000, mean = 350, sd = 20)
but I don't think the rnorm function has a built in "discrete numbers only" option. I have spent a few hours trying to search this on StackOverflow, Google and R documentation but have yet to find anything.
Obviously, there is no discrete normal distribution as by default it is continuous. However, as mentioned here (Wikipedia is not the best possible source but this is correct anyway):
If n is large enough, then the skew of the distribution is not too great. In this case a reasonable approximation to B(n, p) is given by the normal distribution
This can be seen with a quick example:
par(mfrow=c(1,2) )
#values generated by a binomial distribution
plot(density(rbinom(1000, 30, p=0.25)))
#values generated by a normal distribution
plot(density(rnorm(1000)))
Plot:
The graph on the left (binomial) certainly approximates the right (normal) and this will get more obvious as n goes to Inf.
As you will see rbinom(1000, 30, p=0.25) will produce discrete values (integers). Also, density is probably not the best function to use on a discrete variable, but it proves the point here.
if you want to generate a set of random integers following a normal distribution you could simply round them like so...
round(rnorm(10, 5, 1), 0)
library(extraDistr)
set.seed(12)
rdnorm(10)
This question already has answers here:
How do I best simulate an arbitrary univariate random variate using its probability function?
(4 answers)
Closed 8 years ago.
For a normalized probability density function defined on the real line, for example
p(x) = (2/pi) * (1/(exp(x)+exp(-x))
(this is just an example; the solution should apply for any continuous PDF we can define) is there a package in R to simulate from the distribution? I am aware of R's built-in simulators for many distributions.
I could numerically compute the inverse cumulative distribution function at a set of quantiles, store them in a table, and use the table to map from uniform variates to variates from the desired distribution. Is there already a package that does this?
Here is a way using the distr package, which is designed for this.
library(distr)
p <- function(x) (2/pi) * (1/(exp(x)+exp(-x))) # probability density function
dist <-AbscontDistribution(d=p) # signature for a dist with pdf ~ p
rdist <- r(dist) # function to create random variates from p
set.seed(1) # for reproduceable example
X <- rdist(1000) # sample from X ~ p
x <- seq(-10,10, .01)
hist(X, freq=F, breaks=50, xlim=c(-5,5))
lines(x,p(x),lty=2, col="red")
You can of course also do this is base R using the methodology described in any one of the links in the comments.
If this is the function that you're dealing with, you could just take the integral (or, if you're rusty on your integration rules like me, you could use a tool like Wolfram Alpha to do it for you).
In the case of the function provided, you can simulate with:
draw.val <- function(numdraw) log(tan(pi*runif(numdraw)/2))
A histogram confirms that we're sampling correctly:
hist(draw.val(10000), breaks=100, probability=T)
x <- seq(-10, 10, .001)
lines(x, (2/pi) * (1/(exp(x)+exp(-x))), col="red")
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
R, correlation: is there a func that converts a vector of nums to a vector of standard units
By reading stackoverflow's comments, I found z-score maybe calculated with Python or perl, but I did not comes across any for R yet. Did I miss it? Is it possible to be done with R?
As (http://en.wikipedia.org/wiki/Standard_score.)
z-score = (x-μ)/σ
x is a raw score to be standardized;
μ is the mean of the population;
σ is the standard deviation of the population.
I believe there are R packages designed for this? Where can we found them? Or similar package for normalization?
if x is a vector with raw scores then scale(x) is a vector with standardized scores.
Or manually: (x-mean(x))/sd(x)