R Intruction
-
Upload
richard-chen -
Category
Documents
-
view
8 -
download
0
description
Transcript of R Intruction
Enjoy R!
Where to download and get help: http://cran.r-project.org/ You may also try Notepad++. Here is a link with good explanation: http://jekyll.math.byuh.edu/other/howto/notepadpp/using.shtml See what’s there: > ls( ) Remove everything: > rm(list=ls(all=TRUE)) Basic inputs: > a <- c(3, 5, 9, 0) > a [1] 3 5 9 0 > a[3] [1] 9 > b=3 > a+b [1] 6 8 12 3 > d<- c(1,1,1,1) > a+d [1] 4 6 10 1 > a*d [1] 3 5 9 0 > sin(a) [1] 0.1411200 -0.9589243 0.4121185 0.0000000 > a^b [1] 27 125 729 0 > a*d [1] 3 5 9 0 > seq(1,3, 0.25) [1] 1.00 1.25 1.50 1.75 2.00 2.25 2.50 2.75 3.00 R functions for probability distributions:
Every distribution that R handles has four functions. There is a root name, for example, the root name for the normal distribution is norm. This root is prefixed by one of the letters
p for "probability", the cumulative distribution function (c. d. f.) q for "quantile", the inverse c. d. f. d for "density", the density function (p. m.f. or p. d. f.) r for "random", a random variable having the specified distribution
Distribution Functions
Beta pbeta qbeta dbeta rbeta
Binomial pbinom qbinom dbinom rbinom
Cauchy pcauchy qcauchy dcauchy rcauchy
Chi-Square pchisq qchisq dchisq rchisq
Exponential pexp qexp dexp rexp
F pf qf df rf
Gamma pgamma qgamma dgamma rgamma
Geometric pgeom qgeom dgeom rgeom
Hyper geometric phyper qhyper dhyper rhyper
Logistic plogis qlogis dlogis rlogis
Log Normal plnorm qlnorm dlnorm rlnorm
Negative Binomial pnbinom qnbinom dnbinom rnbinom
Normal pnorm qnorm dnorm rnorm
Poisson ppois qpois dpois rpois
Student t pt qt dt rt
Studentized Range ptukey qtukey dtukey rtukey
Uniform punif qunif dunif runif
Weibull pweibull qweibull dweibull rweibull
Wilcoxon Rank Sum Statistic pwilcox qwilcox dwilcox rwilcox
Wilcoxon Signed Rank Statistic psignrank qsignrank dsignrank rsignrank
%%%% c.d.f of binomial with parameters 100 and 0.3 at 30
> pbinom(30, size=100, prob=0.3, lower.tail = TRUE) [1] 0.5491236
> pbinom(30, size=100, prob=0.3, lower.tail = FALSE)
[1] 0.4508764 %%%%%%%%%%%% perform 10,000 binomial experiments > random=rbinom(10000, 100, 0.3) > hist(random)
> hist(random, freq=FALSE)
Histogram of random
random
Frequency
15 20 25 30 35 40 45
0500
1000
1500
> hist(random, freq=FALSE, xlab="TEST", ylab=NULL, main="Today")
> hist(random, freq=FALSE) > lines(0:100, dbinom(0:100, 100, 0.3), col="red")
Histogram of random
random
Density
15 20 25 30 35 40 45
0.00
0.02
0.04
0.06
0.08
Today
TEST
15 20 25 30 35 40 45
0.00
0.02
0.04
0.06
0.08
Normal distribution: Cumulative distribution function of the normal distribution: For X~N(5, 2), is
> pnorm(5, mean=5, sd=sqrt(2))
[1] 0.5 Find such that :
> qnorm(0.95, mean=5, sd=sqrt(2))
[1] 7.326174 For standard normal
> qnorm(0.95)
[1] 1.644854
> pnorm(1.96)
[1] 0.9750021
Histogram of random
random
Density
15 20 25 30 35 40 45
0.00
0.02
0.04
0.06
0.08
Showing the effect of variance: > plot(seq(-7,7, 0.25), dnorm(seq(-7,7, 0.25),mean=0,sd=1), col="red", type="l", + xlab="", ylab="", main="Effect of variance") > lines(seq(-7,7, 0.25), dnorm(seq(-7,7, 0.25),mean=0,sd=1.5), col="blue") > lines(seq(-7,7, 0.25), dnorm(seq(-7,7, 0.25),mean=0,sd=2), col="green")
-6 -4 -2 0 2 4 6
0.0
0.1
0.2
0.3
0.4
Effect of variance