Presentation is loading. Please wait.

Presentation is loading. Please wait.

STAT 251 Lab 1. Outline Lab Accounts Introduction to R.

Similar presentations


Presentation on theme: "STAT 251 Lab 1. Outline Lab Accounts Introduction to R."— Presentation transcript:

1 STAT 251 Lab 1

2 Outline Lab Accounts Introduction to R

3 Session L1E : Th 8-10 @ LSK 121 Session L1F : Th 13-15 @ LSK 121 TA : Eric email : ericfu@stat.ubc.caericfu@stat.ubc.ca www.stat.ubc.ca  people Course Website: http://slate.stat.ubc.ca/  2009   Summer Term 1 and 2   STAT251

4 Lab Account Select any one of the Window 2000 server Press Ctrl+\ and return to the ezConnect Manager if you are in a Unix login page. User name: First 8 letter of your full name registered at UBC e.g. Steven Jason Greenly  stevenja Simply use you full name if it is fewer than 8 letters Password: “S” + first 7 digits of your student ID number e.g. UID:12345678  S1234567 *Capital letter “S”

5 Lab Account CHANGE YOUR PASSWORD once you have logged in Press Ctrl+Alt+Del That would bring you back to the Window login page Option at bottom right for changing password. Please come to the front with your student card for help if you have any problems with the computer.

6 Introduction to R

7 R : a free statistical programming language Q: Why do I need to learn [R] A: assignments will require the use of this software! A: don’t want to do tedious calculation by hand!

8 Introduction to R Downloading R: Available at http://www.r-project.org Or, simply google [R], it should appear as the first hit

9

10

11 Introduction to R Click open [R] from your desktop Icon : The R-console

12 Introduction to R R : Object Oriented Programming (OOP) language We play around with “Objects” / ”Variables” e.g. Vectors, Matrices, Dataframes 3 Components of an Object: Name Class e.g. numeric, character Information

13 Introduction to R How to create vectors

14 Function : c() “c” stands for concatenation most primitive way to create a vector e.g. >c(1, 2, 3)numeric data [1] 1 2 3- Integers >c(‘a’, ‘b’, ‘c’)nonnumeric data [1] “a” “b” “c”- Characters

15 Creating Vectors Q : I want to use the same vector again… A : Give it a name and store it as a variable!

16 Creating Vectors Assignment operator <-OR = e.g. >myvect <- c(1, 2, 3, 4, 5, 6, 7) OR >myvect = c(1, 2, 3, 4, 5, 6, 7)

17 Creating Vectors Now recall the variable myvect >myvect [1] 1 2 3 4 5 6 7 *Variable name starts with letters contains letters, numeric and period “.” only CASE SENSITIVE!

18 Creating Vectors Other functions to create a vector…

19 Function : the colon x:y creates a sequence x:y starts from x, increases by 1 up to y e.g. >myvect2 <- 8:14 >myvect2 [1] 8 9 10 11 12 13 14

20 Creating Vectors Q : How about a sequence of odd numbers?

21 More flexible way to create sequence seq(x, y, k) Starts from x Increase by k up to y e.g. >myvect3 <- seq(1, 14, 2) >myvect3 [1] 1 3 5 7 9 11 13 Function : seq()

22 Function : rep() “rep” stands for replicate rep(x, b) A vector consists of b replicates of x e.g. >rep(0, 5) [1] 0 0 0 0 0 >rep( c(1, 2), 2) [1] 1 2 1 2 >rep(‘a’, 3) [1] “a” “a” “a”

23 Introduction to R Combining vectors

24 Combining Vectors Recall that the function c() links data together e.g. >combine <- c(myvect, myvect2) >myvect [1] 1 2 3 4 5 6 7 >myvect2 [1] 8 9 10 11 12 13 14 >combine [1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14

25 Introduction to R Manipulating vectors

26 Vector manipulation The square brackets [ ] are most oftenly used e.g. To select the 2 nd element in combine >combine[2] [1] 2

27 Vector manipulation e.g. To select from the 2 nd up to the 5 th elements >combine[2:5] [1] 2 3 4 5

28 Vector manipulation e.g. To drop the 3 rd element >combine[ -3 ] [1] 1 2 4 5 6 7 8 9 10 11 12 13 14

29 More examples

30 Vector manipulation e.g. To select from the 1 st, 5 th and then 4 th elements >combine[ c(1, 5, 4) ] [1] 1 5 4

31 Vector manipulation e.g. To drop the 2 nd, 3 rd and then 5 th elements >combine[ -c(2, 3, 5) ] [1] 1 4 6 7 8 9 10 11 12 13 14

32 Introduction to R Basic Mathematical Functions in R

33 Basic math functions 2 + 3 # addition 2 – 3# substraction 2 * 3 # multiplication 2 / 3 # division 2 ^ 3 # power log( 5 ) # natural logarithms sqrt( 2 ) # square root exp( 0.2 ) # exponential function abs( -7 ) # absolute value

34 Basic math functions When applied to a vector, the values are evaluated element-wise e.g. >myvect [1] 1 2 3 4 5 6 7 >myvect^2 [1] 1 4 9 16 25 36 49

35 Basic math functions We can also add, subtract, or multiply two or more vectors of the same length e.g. >myvect [1] 1 2 3 4 5 6 7 >myvect2 [1] 8 9 10 11 12 13 14 >myvect + myvect2 [1] 9 11 13 15 17 19 21

36 Caution! Multiplying Vectors

37 Basic math functions Instead of returning the inner product, the operator * multiplies vectors element-wise e.g. >myvect [1] 1 2 3 4 5 6 7 >myvect2 [1] 8 9 10 11 12 13 14 >myvect * myvect2 [1] 8 18 30 44 60 78 98

38 Advanced & Optional

39 Basic math functions To calculate the inner product operator : %*% e.g. >myvect %*% myvect2 [1] 336 Remark : the data returned is no longer a vector of the same length

40 Basic math functions Again, if you want to reuse the results, store it to a new variable!

41 Introduction to R Basic Statistical Functions in R

42 Basic stat functions sum()#Total sum mean()#Mean median()#Median var()#Variance sd()#Standard Deviation max()#Maximum min()#Minimum range()#Range quantile()#Quantiles (for find IQR) summary()#Similiar to quantiles()

43 Basic stat functions [R] actually has a command call IQR() Its function is self-explanatory, isn’t it?

44 Basic Statistical Functions in R Review of basic statistics

45 Measure of location / central tendency Mean, Median Measure of spread / dispersion Variance, IQR * IQR = 3 rd quantile – 1 st quantile which a number, not an interval !!!

46 Introduction to R Graphics in R

47 A picture is worth a thousand words

48 Visualize data by graphs!

49 Graphics Scatterplot Boxplot Boxplot Histogram Histogram Pay special attention to the last two as they are important for analyzing data for distributions and outliers (To be continued in Lab2)

50 Scatterplot Function : plot(x, y) plot each value in the dataset based on sequential order e.g. >x <- rnorm(20) >y <- 3*x + rnorm(20) >plot(x, y) Generate 20 data randomly from normal distribution. Will cover this function in later stage Graphics

51

52 Attention ! The computers in this lab response slow when the R graphic device is on. Close the graphics device or save the plot if they will be reused You can save your file in the Z: drive Files stred in other places will be deleted when you log out

53 Graphics Histogram Function : hist(x) Plot a histogram of data x e.g. >hist(x)

54

55 Graphics Boxplot Function : boxplot(x) Generate a boxplot of data x e.g. >boxplot(x)

56 Median 3 rd quartile 1 st quartile Potential outlier

57 Introduction to R Quitting and saving your data in R

58 Saving and Quitting To quit Command : q()OR Click on the “x” on the top right hand corner of the R console

59 Saving and Quitting You will see a prompt “Save workspace image?” Click “Yes” if you want to save the variables

60 Yet, your syntax would not be saved!!!

61 SOLUTIONS

62 Saving the syntax 1. Type you syntax in a word editor, e.g notepad. Copy and paste to the R to run the commands. 2. In [R], click File  New script Type in the R editor, highlight and press Ctrl+R to run the selected part. OR Copy and Paste To save the scripts, click to activate the editor window, then File  Save

63 Introduction to R Searching Help for R commands

64 Help! R help files from web Check the r-project website R Books See the posted lab

65 Wake up

66 Bring this home.

67 R help system

68 HELP! You want additional information about a particular function, say read.table() Use “?” or command help() e.g. >?read.table >help(read.table)

69 Pops up the [R] help document

70

71 Let’s call it a day~

72


Download ppt "STAT 251 Lab 1. Outline Lab Accounts Introduction to R."

Similar presentations


Ads by Google