R CODE Name CSC315 Lab 4 Probability Add R code to th

R CODE

########################################################
# Name:
# CSC-315
# Lab #4: Probability
########################################################

##########################################################################
# Add R code to the script below and create a Notebook to complete
# the steps and explicitly answer the following questions
##########################################################################

# Basic Probability Questions. You do not need to use R for these,
# but you should put the answers in your R script.

# A standard deck of cards contains 52 cards, with 13 cards from each
# suit (hearts, clubs, diamonds, and spades).

#1. If one card is selected at random, what is the probability that it
# is the ace of spades?
#2. What is the probability that it is NOT the ace of spades?
#3. What is the probability that it is an ace?
#4. What is the probability that it is an ace or a 4?

# You should use R to answer the remaining questions.

# A probability distribution of a discrete random variable gives
# the probability for each value of the variable.

# Use the \'count.heads\' function below to answer the first question

################################################################
# this function counts the number of heads in \'n\' coin tosses
################################################################
count.heads <-function(n) {
s = sample(c(\"H\", \"T\"), n, replace = TRUE)
num = sum(s==\"H\")
return(num)
}

  
#5. Find the empirical distribution of X = the number of heads in 3
# coin tosses by first using the replicate function to create a
# vector of X values in 1000 experiments. Then create a relative
# frequency table (which approximates the probability distribution table) and
# also create a bar graph of the relative frequencies, making sure to
# appropriately label the x-axis, y-axis, and title of the graph. Also
# set range of the y-axis by specifying the argument ylim = c(0,1).
# Note: your results should roughly match the theoretical probabilities
# which are P(X=0) = 0.125, P(X=1) = 0.375, P(X=2) = 0.375, and
# P(X = 3) = 0.125


#6. Repeat the previous exercise but use the \'permutations\' function from
# the package \'gtools\' to calculate the classical probability distribution
# table for X, which will give you the theoretical probabilities above
# (Note: you do not need to construct a graph for this one).

#7. If you roll two dice (each with the values 1-6), what is the
# probability that the sum of the numbers is 7? Answer this question
# by first writing a function that randomly rolls two dice and then
# calculates the sum. Then use the \'replicate\' function to find the
# empirical probability based on 5000 experiments.

#8. Repeat the previous exercise but use the \'permutations\' function to
# first enumerate the sample space obtained by rolling two dice.


# Poker Time! The commands below generate all possible poker hands.
# Here we ignore the suit since this is not important for our
# calculations. We also use combinations instead of
# permutations to create the sample space. With combinations the order
# (e.g., of cards in a hand) does not matter. The cards are
# also sampled without replacement (repeats.allowed = FALSE by default).
# We also specify \'set = FALSE\' to allow for duplicate values in the deck
# vector. Finally, each combination is equally likely, so classical
# probability can be used.

deck = rep(1:13,4)
hands = combinations(52, 5, deck, set = FALSE)

#9. How many possible poker hands are there?

##############################################################
# this function returns true if a hand contains a 4-of-a-kind
##############################################################
four.of.a.kind <- function(x) {
t = table(x)
m = max(t)
if (m == 4) return (TRUE)
return (FALSE)
}

#10. Show that the probability of being dealt a four-of-a-kind is
# approximately 0.00024 (or 1/4165). Note: You MUST use the
# apply function and the four.of.a.kind function above to find this.
# Because the hands matrix contains more than
# 2.5 million rows, even with the apply function this
# calculation will take time (5-10 minutes). You may therefore want to test
# on a subset of the hands matrix first. There are two 4-of-a-kinds
# if you look at the first 20,000 rows

#11. Create a function that determines whether a vector \'x\' contains
# a full house (i.e., 3 of a kind and 1 pair). You can assume
# that \'x\' represents 5 cards.

#12. Show that the probability of being dealt a full house is
# approximately 0.00144 (roughly 1/694). In the first
# 20,000 rows of the hands matrix, there are 18 full houses.

Solution

IDENTIFICATION DIVISION.
PROGRAM-ID. PROG-ONE.
AUTHOR. MARKIED JONES.


FD PATRON-LIST.
01 PATRON-LINE PIC X(80).



surroundings DIVISION.   

INPUT-OUTPUT SECTION.   
FILE-CONTROL.   
choose PATRON-FILE ASSIGN TO INFILE.   
choose PATRON-LIST ASSIGN TO OUTFILE.

knowledge DIVISION.

FILE SECTION.   
FD PATRON-FILE.
01 PATRON-RECORD.
03 PR-NAME PIC X(18).   
03 PR-ADDRESS PIC X(18).   
03 PR-CITY-STATE-ZIP PIC X(24).   
03 PR-TARGET-CONTR PIC 9(4).
03 PR-ACTUAL-CONTR PIC 9(4).
03 PR-CONTR-DATE.   
05 PR-CONTR-MONTH PIC XX.
05 PR-CONTR-DAY PIC XX.
05 PR-CONTR-YEAR PIC XX.
03 FILLER PIC X(6).
  
  
03 FILLER PIC X.
  
  
  
  
  
  
  
  
  
WORKING-STORAGE SECTION.   
  
01 PATRON-LINE-1.
03 FILLER PIC X.
03 PL-NAME PIC X(18) .
03 FILLER PIC X.
03 PL-ADDRESS PIC X(18).
03 FILLER PIC XX.   
03 PL-CITY-STATE-ZIP PIC X(24).
  
01 WS-HEADER-LINE-1.   
  
03 FILLER PIC X(18) price areas.   
03 FILLER PIC X(7) price \"INDIANA\".
03 FILLER PIC X price area.
03 FILLER PIC X(10) price \"UNIVERSITY\".   
03 FILLER PIC X price area.
03 FILLER PIC X(2) price \"OF\".   
03 FILLER PIC X price area.
03 FILLER PIC X(12) price \"PENNSYLVANIA\".
  
01 WS-HEADER-LINE-1-UL.
  
03 FILLER PIC X(18) price areas.   
03 FILLER PIC X(7) price \"-\".
03 FILLER PIC X price area.
03 FILLER PIC X(10) price \"-\".
03 FILLER PIC X price area.
03 FILLER PIC X price \"-\".

01 HEADER-LINE-2.
03 FILLER PIC X(20) price areas.   
03 FILLER PIC X(8) price \"COMPUTER\".   
03 FILLER PIC X price area.
03 FILLER PIC X(7) price \"SCIENCE\".
03 FILLER PIC X price area.
03 FILLER PIC X(10) price \"DEPARTMENT\".
01 HEADER-LINE-2-UL.   
03 FILLER PIC X(20) price areas.   
03 FILLER PIC X(8) price ALL \"-\".
03 FILLER PIC X price area.
03 FILLER PIC X(7) price ALL \"=\".
03 FILLER PIC X price area.
03 FILLER PIC X(10) price ALL \"-\".
01 HEADER-LINE-3.
03 FILLER PIC X(22) price areas.   
03 FILLER PIC X(7) price \"LISTING\".
03 FILLER PIC X price area.
03 FILLER PIC X(2) price \"OF\".   
03 FILLER PIC X price area.
03 FILLER PIC X(7) price \"PATRONS\".
01 HEADER-LINE-3-UL.   
03 FILLER PIC X(22) price areas.   
03 FILLER PIC X(7) price \"-\".
03 FILLER PIC X price area.
03 FILLER PIC X(6) price \"-\".
03 FILLER PIC X(15) price areas.   
03 FILLER PIC X(6) price \"-\".
01 WS-COLUMN-HEADER-2.
03 FILLER PIC X(4) price areas.   
03 FILLER PIC X(4) price \"NAME\".   
03 FILLER PIC X(8) price areas.   
03 FILLER PIC X(7) price \"ADDRESS\".
03 FILLER PIC X(12) price areas.   
03 FILLER PIC X(14) price \"CITY-STATE-ZIP\".   
01 WS-COLUMN-HEADER-2UL.
03 FILLER PIC X(4) price areas.   
03 FILLER PIC X(4) price \"-\".
03 FILLER PIC X(8) price areas.   
03 FILLER PIC X(7) price \"-\".
03 FILLER PIC X(12) price areas.   
03 FILLER PIC X(14) price \"-\".
01 WS-FOOTER-LINE-1.
03 FILLER PIC X(24) price   
\"TOTAL # OF PATRONS:\".   
03 WS-NUM-PATRONS PIC Z9.   
01 WS-FOOTER-LINE-2.
03 FILLER PIC X(32) price   
\"PROGRAMMED BY A COSC 220 STUDENT\".
  
  
  
01 WS-EOF-SWITCH PIC X(80).
PROCEDURE DIVISION.
  
000-PRINT-PATRON-LIST.   
OPEN INPUT PATRON-FILE   
OUTPUT PATRON-LIST
MOVE \"NO\" TO WS-EOF-SWITCH   
scan PATRON-FILE   
AT finish MOVE \"YES\" TO WS-EOF-SWITCH
END-READ   
PERFORM 100-PROCESS-PATRON-RECORD
till WS-EOF-SWITCH IS adequate to \"YES\"
shut PATRON-FILE PATRON-LIST
STOP RUN   
.
  
050-WRITE-HEADERS-PARA.
WRITE PATRON-LINE FROM WS-HEADER-LINE-1
WRITE PATRON-LINE FROM WS-HEADER-LINE-1-UL   
WRITE PATRON-LINE FROM WS-HEADER-LINE-2-UL
WRITE PATRON-LINE FROM WS-HEADER-LINE-3   
WRITE PATRON-LINE FROM WS-HEADER-LINE-3-UL
MOVE areas TO PATRON RECORD
  
WRITE PATRON-LINE FROM COLUM-HEADER-1   
WRITE PATRON-LINE FROM COLUM-HEADER-1-UL
WRITE PATRON-LINE FROM WS-COLUM-HEADER-2
WRITE PATRON-LINE FROM WS-COLUM-HEADER-2-UL   
WRITE PATRON-LINE FROM WS-FOOTER-LINE-1   
WRITE PATRON-LINE FROM WS-FOOTER-LINE-2   
  
MOVE areas TO PATRON RECORD
WRITE PATRON RECORD   
.   
100-PROCESS-PATRON-RECORD.
MOVE PR-NAME TO PL-NAME   
MOVE PR-ADDRESS TO PL-ADDRESS   
MOVE PR-CITY-STATE-ZIP TO PL-CITY-STATE-ZIP   
WRITE PATRON-LINE
scan PATRON-FILE
AT finish MOVE \"YES\" TO WS-EOF-SWITCH   
END-READ   
.   

Errors:
144 IGYPS2121-S \"PATRON-LINE\" wasn\'t outlined as a data-name. The stateme

Same message on line: a hundred forty five 146 147 148 149

146 IGYPS2121-S \"WS-HEADER-LINE-2\" wasn\'t outlined as a data-name. The st

147 IGYPS2121-S \"WS-HEADER-LINE-2-UL\" wasn\'t outlined as a data-name. The

148 IGYPS2121-S \"WS-HEADER-LINE-3\" wasn\'t outlined as a data-name. The st

149 IGYPS2121-S \"WS-HEADER-LINE-3-UL\" wasn\'t outlined as a data-name. The

150 IGYPS2121-S \"PATRON\" wasn\'t outlined as a data-name. The statement American state

Same message on line: 159 a hundred and sixty   

150 IGYPS2072-S \"RECORD\" was invalid. Skipped to succeeding verb, period or

Same message on line: 159 a hundred and sixty   

152 IGYPS2121-S \"COLUM-HEADER-1\" wasn\'t outlined as a data-name. The stat

153 IGYPS2121-S \"COLUM-HEADER-1-UL\" wasn\'t outlined as a data-name. The s

154 IGYPS2121-S \"WS-COLUM-HEADER-2\" wasn\'t outlined as a data-name. The s

155 IGYPS2121-S \"WS-COLUM-HEADER-2-UL\" wasn\'t outlined as a data-name. Th

R CODE ######################################################## # Name: # CSC-315 # Lab #4: Probability ########################################################
R CODE ######################################################## # Name: # CSC-315 # Lab #4: Probability ########################################################
R CODE ######################################################## # Name: # CSC-315 # Lab #4: Probability ########################################################
R CODE ######################################################## # Name: # CSC-315 # Lab #4: Probability ########################################################
R CODE ######################################################## # Name: # CSC-315 # Lab #4: Probability ########################################################

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site