-
Notifications
You must be signed in to change notification settings - Fork 0
/
R-code
144 lines (106 loc) · 3.39 KB
/
R-code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
**SCENARIO 1**
# Create list of data
rbcc <- c(5.4, 5.2, 5.0, 5.2, 5.5, 5.3, 5.4, 5.2, 5.1, 5.3, 5.3, 4.9, 5.4, 5.2, 5.2)
# Plotting histogram
hist(rbcc, breaks=8, col="lightblue", main="Histogram of RBC Counts", xlab="Red Blood Cell Count (10^6 cells/L)", border="black", labels = T)
# Plotting boxplot
boxplot(rbcc, horizontal=TRUE, main="Boxplot of RBC Counts", ylab="Red Blood Cell Count (10^6 cells/L)", col = "lightblue")
mean_rbcc <- mean(rbcc)
median_rbcc <- median(rbcc)
mean_rbcc
median_rbcc
std_dev <- sd(rbcc)
std_devs_away <- (5.7 - mean_rbcc) / std_dev
std_dev
std_devs_away
**SCENARIO 2**
pulse_rates <- c(80, 70, 88, 70, 84, 66, 84, 82, 66, 42, 52, 72, 90, 70, 96, 84, 96, 86, 62, 78,
60, 82, 88, 54, 66, 66, 80, 88, 56, 104, 84, 84, 60, 84, 88, 58, 72, 84, 68, 74,
84, 72, 62, 90, 72, 84, 72, 110, 100, 58)
# Creating a stem-and-leaf plot with each stem split into two lines
stem(pulse_rates, scale=2)
# Creating a relative frequency histogram
hist(pulse_rates, freq=FALSE, col="lightblue", border="black", main="Relative Frequency Histogram of Pulse Rates", xlab="Pulse Rate")
# Calculating summary statistics for the pulse rates
summary_stats <- summary(pulse_rates)
iqr_val <- IQR(pulse_rates)
summary_stats
iqr_val
**SCENARIO 3**
# Given parameters
mu <- 12
sigma <- 2.3
# a. 9.7 to 14.3 breaths per minute
z1_a <- (9.7 - mu) / sigma
z2_a <- (14.3 - mu) / sigma
prob_a <- pnorm(z2_a) - pnorm(z1_a)
# b. 7.4 to 16.6 breaths per minute
z1_b <- (7.4 - mu) / sigma
z2_b <- (16.6 - mu) / sigma
prob_b <- pnorm(z2_b) - pnorm(z1_b)
# c. More than 18.9 or less than 5.1 breaths per minute
z1_c <- (5.1 - mu) / sigma
z2_c <- (18.9 - mu) / sigma
prob_c <- pnorm(z1_c) + (1 - pnorm(z2_c))
prob_a * 100
prob_b * 100
prob_c * 100
**SCENARIO 4**
# Given data
armspan <- c(68, 62.25, 65, 69.5, 68, 69, 62, 60.25)
height <- c(69, 62, 65, 70, 67, 67, 63, 62)
# a. Scatterplot of armspan vs. height
plot(armspan, height, main="Scatterplot of Armspan vs. Height", xlab="Armspan (inches)", ylab="Height (inches)", xlim=c(60, 70), ylim=c(60, 70), pch=16, col="blue")
# b. Correlation coefficient
cor_coeff <- cor(armspan, height, method = "pearson")
# c. Estimate the slope of the regression line
slope <- cor_coeff * (sd(height) / sd(armspan))
# d. Calculate the regression line
intercept <- mean(height) - slope * mean(armspan)
# e. Predict height for an armspan of 62 inches
predicted_height <- intercept + slope * 62
cor_coeff
slope
intercept
predicted_height
**SCENARIO 5**
# Given data
nA <- 154
nB <- 67
nC <- 35
nP <- 101
nG <- 155
nA_and_P <- 66
nA_and_G <- 88
nB_and_P <- 23
nB_and_G <- 44
nC_and_P <- 12
nC_and_G <- 23
n <- 256
# Calculating probabilities
pA <- nA / n
pG <- nG / n
pA_and_G <- nA_and_G / n
pG_given_A <- nA_and_G / nA
pG_given_B <- nB_and_G / nB
pG_given_C <- nC_and_G / nC
pC_given_P <- nC_and_P / nP
pB_complement <- 1 - (nB / n)
cat(pA, pG, pA_and_G, pG_given_A, pG_given_B, pG_given_C, pC_given_P, pB_complement)
**SCENARIO 6**
# Given values
mean_banana = 630 # mean potassium in one banana
std_dev_banana = 40 # standard deviation
n = 3 # number of bananas
# Mean and standard deviation of T
mean_T = n * mean_banana
std_dev_T = sqrt(n) * std_dev_banana
# Probability that T exceeds 2000 mg
# Standardize the variable (2000 mg) first
z = (2000 - mean_T) / std_dev_T
# Find the probability
probability = 1 - pnorm(z)
# Output the results
mean_T
std_dev_T
probability