Chapter I: Basics of R - DCU School of Computing

Download Report

Transcript Chapter I: Basics of R - DCU School of Computing

1. Basics of R
CA200
Dublin City University
(based on the book by Prof. Jane M. Horgan)
1
Installing R
• Go to the Cran website at http://cran.rproject.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 :)
CA200
2
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
CA200
3
Basics
– 6+7*3/2
[1] 16.5
– x <- 1:4
x
[1] 1 2 3 4
– x2 <- x**2
x2
[1] 1 4 9 16
#general expression
– X <- 10
prod1 <- X*x
prod1
[1] 10 20 30 40
#case sensitive!
#integers are assigned to the vector x
#print x
#square the element, or x2<-x^2
CA200
4
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.
CA200
5
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”
CA200
6
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
CA200
7
2. Basic probability with R
CA200
8
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)
CA200
9
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
Permutations
The number of ordered sequences where repetition is not allowed, i.e. no element can
appear more than once in the sequence.
Ordered samples (sequences) of size k from n:
𝑛
𝑃𝑘 = 𝑛 𝑛 − 1 … 𝑛 − 𝑘 + 1 =
𝑛!
𝑛−𝑘 !
In R use function:
prod
Example 1. Three elements {1,2,3}. How many sequences of two elements from these three?
Example 1 - Solution:
(1,2); (1,3); (2,1); (2, 3); (3,1); (3,2)  Six ordered sequences altogether.
3P = 3 * 2 = 6
2
Solution is R:
prod (3:2)
[1] 6
CA200
11
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?
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.
4P
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.
4P
3
= 4 *3 * 2= 24
In R:
prod (4:2)
[1] 24
12
Combinations
The number of unordered sets of distinct elements, i.e. repetition is not allowed.
Number of ways of selecting k distinct elements from n or equivalently number of
unordered samples of size k, without replacement from n
𝑛
𝐶𝑘 =
𝑛
𝑃𝑘
𝑘!
= 𝑘!
𝑛!
𝑛−𝑘 !
In R use function:
choose
Example 4. Three elements {1,2,3}. How many sets (combinations) of two elements from these
three?
Example 4 - Solution:
{1,2}; {1,3}; {2,3}  Three ordered sequences altogether.
3C
2
= (3*2)/2*1 = 3
In R:
choose (3,2)
[1] 3
13
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.
Q5-solution:
{1,2}; {1,3}; {1,4} {2,3}; {2, 4}; {3,4}  Six unordered sequences altogether.
4C
2
= (4*3)/(2*1) = 6
In R:
choose (4, 2)
[1] 6
Q6-solution:
{1,2,3}; {1,2,4}; {2,3,4}; {3,1,4}  Four unordered sequences.
4C
3
= (4*3*2)/(3*2*1)= 4
In R:
choose(4, 3)
[1] 4
Q7-Solution:
E – all chips are good
𝑃 𝐸 =
75
12
100
12
In R:
𝑃 𝐸 =
𝑐ℎ𝑜𝑜𝑠𝑒(75,12)
𝑐ℎ𝑜𝑜𝑠𝑒(100,12)
14
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
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
15
[1] 0.5294118
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
Probability
5
0.3
6
0.7
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?
CA200
16
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
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
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
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
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