122k views
2 votes
Math 2265 R-programming in posit Cloud

I fix the coding of the assignment for project 2 but I need double check or fix my coding for my project 2 in order for me to knit the assignment. I need help programming from question 1, 2 , 3, and 4.
cards_data <- read.csv("cards_data.csv")
print(cards_data)
diamond_freq <- table(cards_data$Diamond_Cards)
print(diamond_freq)
# Create a relative frequency distribution of the number of diamond cards selected
diamond_rel_freq <- prop.table(diamond_freq)
# Print the relative frequency table
print(diamond_rel_freq)
# Create a relative frequency distribution of the number of diamond cards selected as percentages
diamond_rel_freq_pct <- prop.table(diamond_freq) * 100
# Print the relative frequency table as percentages
print(diamond_rel_freq_pct)
### 2. Create a graph of the probability distribution. Make sure that your graph includes all the possible values of the discrete random variable.
```{r}
#Use this R chunk to create your graph.
#Enter the values of the discrete random variable this is the number of times that you can get a diamond card in 10 card selections
x-c(cards_dat)
#Enter the promotion from the relative frequency table. use 0 for any missing promotions
Px<-C(cards_dat)
# Calculate the probabilities
probs <- rep(1/13, 13)
# Create the bar chart
barplot(probs, names.arg = cards, xlab = "Card Value", ylab = "Probability", main = "Probability Distribution of Standard Deck of Cards")
```
### 3. Calculate the mean and the standard of your distribution.
```{r}
#Use this R chunk to calculate the mean and standard deviation.
cards <- c("2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A")
# Calculating standard
# deviation using sd()
print(sd(cards))
```
### 4. Find the following probabilities. (Hint: Use your work from Problem 1.)
**a.) P(x=3)**
**b.) P(1 < x < 4)**
**c.) P(x ≥ 8)**

User Akashbc
by
8.3k points

1 Answer

1 vote
To double-check or fix the coding for your project 2 in order to knit the assignment, you can follow these steps for questions 1, 2, 3, and 4:
1. **Question
1**: The given code reads a CSV file named
"cards_data.cv" and assigns it to the variable 'cards_data. It then creates a frequency table (diamond_freq) based on the column "Diamond_Cards" in cards_data. The relative frequency distribution of the number of diamond cards selected is stored in diamond _rel_freq' and printed.
Finally, the relative frequency distribution is also calculated as percentages and printed as diamond_rel_freq_pct. This code appears to be correct.

2. **Question
2**: The code provided in the R chunk generates a bar plot for the probability distribution.
However, there seem to be some typos in the code. Here's the corrected code:
''R
# Enter the values of the
discrete random variable (number of times a diamond card is selected in 10 card selections)

X <-
cards_data$Diamond_Cards
# Enter the probabilities from
the relative frequency table. Use O for any missing probabilities.
Px <- diamond_rel_freq
# Calculate the probabilities
probs <- rep(1/13, 13)

# Create the bar chart
barplot(probs, names.arg = x,
xlab = "Card Value", ylab =
"Probability", main = "Probability
Distribution of Standard Deck of
Cards")
Make sure to run this code chunk after the previous code to generate the desired graph.
3. **Question 3**: The code in the R chunk calculates the standard deviation of the variable cards' using the 'sd0' function. However, the variable cards' is defined as a character vector containing card names, which is incorrect. To calculate the mean and standard deviation of the distribution, you need to use the variable '× from question 2. Here's the corrected code:

# Calculate the mean and
standard deviation mean_value <- mean(x)
sd_value <- sd(x)
# Print the mean and standard
deviation
print(mean_value)
print(sd_value)
Make sure to run this code chunk after the previous code to obtain the mean and standard deviation.
4. **Question 4**: To find the probabilities, you can use the relative frequency distribution (diamond_rel_freq) obtained in question 1.
a) **P(×=3)**: The probability of selecting exactly 3 diamond cards can be obtained from
'diamond_rel_freq. Look for the relative frequency
corresponding to the value 3.
b) **P(1 < x < 4)**: The probability of selecting more than 1 and less than 4 diamond cards can be calculated by summing the relative frequencies for the values 2 and 3 from diamond_rel_fred.
c) **P(x ≥ 8)**: The probability of selecting 8 or more diamond cards can be obtained by summing the relative
frequencies for the values 8, 9, 10, J, Q, K, and A from diamond_rel_freq.

To find these probabilities, you can use the following code:
"'R
# Probability of ×=3 prob_a <- diamond_re _freq[3]
# Probability of 1 < x < 4
prob_b <- diamond_rel_freq[2] + diamond_rel_freq[37
# Probability of × ≥ 8 prob_c <-
sum(diamond_rel_freq[8:13])
# Print the probabilities
print(prob_a)
print(prob_b)
print(prob_C)
Make sure to run this code after the previous code to calculate the desired probabilities.
User Alex Libov
by
7.8k points
Welcome to QAmmunity.org, where you can ask questions and receive answers from other members of our community.