Brief Introduction to R - People.stat.sfu.ca - Simon Fraser University
Brief Introduction to R - People.stat.sfu.ca - Simon Fraser University
Brief Introduction to R - People.stat.sfu.ca - Simon Fraser University
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
STAT 430 – TUTORIAL 1<strong>Introduction</strong> <strong>to</strong> RJoslin Goh<strong>Simon</strong> <strong>Fraser</strong> <strong>University</strong>Sept. 12, 2007
<strong>Introduction</strong> <strong>to</strong> R• Download from http://cran.<strong>stat</strong>.<strong>sfu</strong>.<strong>ca</strong>/• An R object is any vec<strong>to</strong>r, matrix, list, etc.• To add an object> x ls()• To remove an object, say x> rm(x)• To remove the list> rm(list =ls())
Mathemati<strong>ca</strong>l Calculations• Addition/Subtraction> 5+5>10-2• Multipli<strong>ca</strong>tion/Division> 5*5>10/2
Mathemati<strong>ca</strong>l Calculations• Power multipli<strong>ca</strong>tion> 64^(1965)> sqrt(64)• Exponentials> exp(1)• Trigonometrics> tan(pi)> sin(pi/2)> cos(pi/4)
Mathemati<strong>ca</strong>l Calculations• Logarithms- Natural Logarithm> log(10)- Logarithm with base n> log(10,base=n)
Importing Data• To read data from a text file, say “file1.txt”> d data
Vec<strong>to</strong>rs• To create a vec<strong>to</strong>r of numbers> x1 x2 x3
Matrix• Combine vec<strong>to</strong>rs of numbers> x y A B
Matrix Operations• Addition and subtraction works the same as youthink.• Multipli<strong>ca</strong>tion- A*B element multipli<strong>ca</strong>tion- A%*%B matrix multipli<strong>ca</strong>tion• Transpose>t(A)• Inverse>solve(A)
Distributions• Calculating the pdf> dnorm(x,mean,sd)• Calculating the cdf> pnorm(x,mean,sd)• Determining a quantile, ie inverse cdf> qnorm(x,mean,sd)• Generate a random number> rnorm(how many, mean, sd)
Type of Distributions• Uniform - unif• Binomial - binom• Negative binomial - nbinom• Poisson - pois• Gamma - gamma• Normal - norm• Exponential - exp• Chi-squared - chisq• Student t - t• F - f
Functions• Summation – sum(something)• Mean – mean(something)• Variance – var(something)• Covariance – cov(some, thing)• Correlation – cor(some, thing)
Graphics• His<strong>to</strong>gram> hist(X,xlab="x label",ylab="y label",main="My his<strong>to</strong>gram")• Boxplot>plot(X,xlab="x label",ylab="y label",main="My plot“)• Plot of data according <strong>to</strong> input order>plot(X,xlab="x label",ylab="y label",main="My plot“, type=“l”)
Graphics• Adding a line on the graph> abline(h=?) for horizontal line> abline(v=?) for verti<strong>ca</strong>l line