Download presentation

Presentation is loading. Please wait.

Published byAden Isbell Modified about 1 year ago

1
CA200 Dublin City University (based on the book by Prof. Jane M. Horgan) 1

2
Installing R Go to the Cran website at http://cran.r- project.org/http://cran.r- project.org/ Click ‘Download and Install R ’ Choose an operating system e.g. Windows; Choose the ‘base’ package Select the setup program (e.g. R *.exe) Press the option ‘Run’ R is now installed :) CA2002

3
R Documentation - Manuals http://cran.r-project.org/ An Introduction to R The R Language Definition Writing R Extensions R Data Import/Export R Installation and Administration. R Internals The R Reference Index 3CA200

4
Basics – 6+7*3/2#general expression [1] 16.5 – x <- 1:4#integers are assigned to the vector x x#print x [1] 1 2 3 4 – x2 <- x**2#square the element, or x2<-x^2 x2 [1] 1 4 9 16 – X <- 10 #case sensitive! prod1 <- X*x prod1 [1] 10 20 30 40 4CA200

5
Basics <- assignment operator R is case sensitive - x and X are different values Variables can consist of any combination of cases or numbers but cannot begin with _ or numeral Objects: The entities that R creates and manipulates, e.g. variables, arrays, strings, functions Workspace: All objects created in R are stored in a workspace. 5CA200

6
Getting Help click the Help button on the toolbar help() help.start() demo() ?read.table help.search ("data.entry") apropos (“boxplot”) - "boxplot", "boxplot.default", "boxplot.stat” 6CA200

7
Data Entry Entering data from the screen to a vector Example: downtime <-c(0, 1, 2, 12, 12, 14, 18, 21, 21, 23, 24, 25, 28, 29, 30,30,30,33,36,44,45,47,51) mean(downtime) [1] 25.04348 median(downtime) [1] 25 range(downtime) [1] 0 51 sd(downtime) [1] 14.27164 7CA200

8
8

9
Basic definitions Experiment is a process of observation that leads to a single outcome that cannot be predicted with certainty Examples: 1. Pull a card from a deck 2. Toss a coin 3. Response time Sample Space: All outcomes of an experiment. Usually denoted by S Event denoted by E is any subset of S 1. E = Spades 2. E = Head 3. E = Component is functioning P(E) denotes the probability of the event E 1. P(E) = P(Spades) 2. P(E) = P(Head) 3. P(E) = P(Component is functioning) 9CA200

10
Calculating Probabilities CLASSICAL APPROACH: Assumes all outcomes of the experiment are equally likely: Example: Roll a fair die. E = even number RELATIVE FREQUENCY APPROACH: Interprets the probability as the relative frequency of the event over a long series of experiment. Example: Roll a die a large number of times and observe number of times an even number occurs 10

11
Permutations 11CA200

12
Permutations - examples Q2. Four elements {1,2,3,4}. How many sequences of two elements from these four? Q3. Four elements {1,2,3,4}. How many sequences of three elements from these four? 12 Q2-solution: (1,2); (1,3); (1,4) (2,1); (2, 3); (2,4); (3,1); (3,2); (3,4); (4,1); (4,2); (4,3). Twelve ordered sequences altogether. 4 P 2 = 4 * 3 = 12 In R: prod (4:3) [1] 12 Q3-solution: (1,2,3); (1,3,2); (1,2,4); (1,4,2); (1,3,4); (1,4,3) (2,1,3); (2,3,1); (2,1,4); (2,4,1); (2,3,4); (2,4,3) (3,1,2); (3,2,1); (3,2,4); (3,4,2); (3,1,4); (3,4,1) (4,1,3); (4,3,1); (4,1,2); (4,2,1); (4,3,2); (4,2,3) Twenty-four ordered sequences. 4 P 3 = 4 *3 * 2= 24 In R: prod (4:2) [1] 24

13
Combinations 13

14
Combinations - examples Q5. Four elements {1,2,3,4}. How many combinations of two elements from these four? Q6. Four elements {1,2,3,4}. How many unordered of three elements from these four? Q7. If a box contains 75 good IC chips and 25 defective chips, and 12 chips are selected at random, find the probability that all chips are good. 14

15
Bayes’ rule - example Q8. In a computer installation, 200 programs are written each week, 120 in C++ and 80 in Java. 60% of the programs written in C++ compile on the first run and 80% of the Java programs compile on the first run. a)If a randomly selected program compiles on the 1st run what is the probability that it was written in C++? Q8 – Solution: Lets denote: C++ - event that program is written in C++ J - event that program is written in Java E - event that selected program compiles on the first run P(E) = P(C++)P(E|C++) + P(J)P(E|J) = 120/200 * 72/120 + 80/200*64/80 = 0.68 15 In R: total_prob <- ((80/200)*(64/80)) + ((120/200)*(72/120)) #total probability condit_c <- (120/200)*(72/120)/total_prob #posterior probability of C++ condit_c [1] 0.5294118

16
Solve following exercises using R: (E2.1) A card is drawn from a shuffled pack of 52 cards. What is the probability of drawing a ten or a spade? (E2.2) Records of service requests at a garage and their probabilities are as follows: Daily demand is independent (e.g. tomorrow’s demand is independent of today’s demand). What is the probability that over a two-day period the number of requests will be a) 10 requests, b) 11 requests and c) 12 requests? (E2.3) Analysis of a questionnaire completed by holiday makers showed that 0.75 classified their holiday as good at resort Costa Lotta. The probability of hot weather in this resort is 0.6. If the probability of regarding the holiday as good given hot weather is 0.9, what is the probability that there was hot weather if a holiday maker considers his holiday good? Daily DemandProbability 50.3 60.7 CA200 16

17
Solutions: E2.1 A card is drawn from a shuffled pack of 52 cards. What is the probability of drawing a ten or a spade? E2.1-Solution: p(ten) = 4/52, p(spade) = 13/52 = ¼, p(ten of spades) = 1/52. Therefore, p(drawing a ten or a spade) = 4/52 + 13/52 – 1/52 = 16/52 = 4/13. E2.2 Records of service requests at a garage and their probabilities are as follows: Daily demand is independent (e.g. tomorrow’s demand is independent of today’s demand). What is the probability that over a two-day period the number of requests will be a)10 requests, b) 11 requests and c) 12 requests? E2.2 - Solution: a) p(10 requests) = p(5 and 5) = 0.3*0.3 = 0.09 b) p(11 requests) = p(5 and 6) + p(6 and 5) = 0.3*0.7 + 0.7*0.3 = 0.42 c) p(12 requests) = p(6 and 6) = 0.7*0.7 = 0.49 E2.3 Analysis of a questionnaire completed by holiday makers showed that 0.75 classified their holiday as good at resort Costa Lotta. The probability of hot weather in this resort is 0.6. If the probability of regarding the holiday as good given hot weather is 0.9, what is the probability that there was hot weather if a holiday maker considers his holiday good? E2.3 - Solution: Let H = hot weather (event), G = good holiday (event) => we need p(H|G). We are given p(G) = 0.75, p(H) = 0.6 and p(G|H) = 0.9. Bayes’ theorem => p(H|G) = p(H)p(G|H)/p(G) = 0.6*0.9/0.75 = 0.72 or 72% chance. Daily DemandProbability 50.3 60.7 17

18
Binomial distribution Def: Conditions: 1.An experiment consists of n repeated trials 2.Each trial has two possible outcomes a)A success with probability p is constant from trial to trial b)A failure with probability q = 1 − p 3. Repeated trials are independent X = number of successes in n trials, X is a BINOMIAL RANDOM VARIABLE. The probability of getting exactly x successes in n trials is: In R use function: use prefix “binom” with prefix “d” for pdf. Function is dbinom(x, n, p) 18

19
Binomial - examples Q9. Five terminals on an on-line computer system are attached to a communication line to the central computer system. The probability that any terminal is ready to transmit is 0.95. Q9-solution: Lets denote: X = number of ready terminals p – probability of success in one trial n – number of trials p = 0.95 n = 1 – q = 5 P(X=0) = P(0 ready terminal in 5): FFFFF P(X=1) = P(1 ready terminal in 5): SFFFF, FSFFF, FFSFF, FFFSF, FFFFS In R: For all cumulative probabilities: x <- 0:5 dbinom(x, size = 5, prob = 0.95) [1] 0.0000003125 0.0000296875 0.0011281250 0.0214343750 0.2036265625 0.7737809375 The probability of getting exactly 3 ready terminals from five: dbinom(x = 3, size =5, prob =.95) [1] 0.02143438 19

20
Binomial - examples Q9. Five terminals on an on-line computer system are attached to a communication line to the central computer system. The probability that any terminal is ready to transmit is 0.95. Q9-solution – using plot function plot(x, dbinom(x, size = 5, prob = 0.95), xlab = "Number of Ready Terminals", ylab = "P(X = x)", type = "h", main = "Ready Terminals, n = 5, p = 0.95") 20

21
Binomial - examples Q10. A fair coin is tossed 10 times. Success and failure are “heads” and “tails”, respectively, each with probability, 0.5. Q10-solution: X = number of heads (successes) obtained p = 0.50 n = 10 P(X=0) = P(exactly 0 head in 10 tosses) P(X=1) = P(exactly 1 head in 10 tosses) P(X=2) = P(exactly 2 heads in 10 tosses)… In R: x <- 0:10 round(dbinom(x, 10, 0.5), 4) [1] 0.0010 0.0098 0.0439 0.1172 0.2051 0.2461 0.2051 0.1172 0.0439 0.0098 0.0010 21

22
Binomial - examples Q10. A fair coin is tossed 10 times. Success and failure are “heads” and “tails”, respectively, each with probability, 0.5. Q10 solution – using plot function plot(x, dbinom(x, size = 10, prob = 0.5), xlab = "Number of Heads", ylab = "P(X = x)", type = "h", main = "Heads, n = 10, p = 0.5") 22

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google