R Flashcards
sessionInfo()
provides information about the operating system, version of R and version of all R packages being used.
How to simulate a random sample for a discrete uniform distribution
To generate a vector for sample space S = {1, 2, 3, ….. , 20}: S = 1:20
To simulate 100 values from this sample space: sample(S, 100, replace = TRUE)
distribution functions
The R code for simulating values and calculating probabilities and quantiles from the binomial distribution uses the R functions rbinom, dbinom, pbinom and qbinom. The prefixes r, d, p, and q stand for random generation, density, distribution and quantile functions respectively. (A discrete distribution has a probability mass function rather than a density, but the prefix d is still used.)
Random sample of 100 values from a discrete uniform distribution (1,20)
S=1:20 P=sample(S,100,replace=TRUE)
a) Get a random sample of 100 values from the Bin(20,0.3)
b) Calc P(X=2)
a) ```
n=20
p=0.3
rbinom(100,n,p)
~~~
b) dbinom(2,n,p)
Setting a seed
set.seed(n)
Graphs
hist
curve
Constructing 95% CI for the mean with unknown variance
The R function for a symmetrical 95% confidence interval for the mean with unknown variance is:
t.test(<sample data>, conf=0.95)
For small samples from a non-normal distribution, confidence intervals can be constructed empirically in R using the bootstrap method . For example, a non-parametric 95% confidence interval for the mean could be obtained by:quantile(replicate(1000,mean(sample(<sample data>,replace=TRUE))), probs=c(0.025,0.975))