STAT2102 Midterm1 RMD
STAT2102 Midterm1 RMD
STAT2102 Midterm1 RMD
Nathan Shurts
2024-10-07
##Problem 1
#Question 1
## [1] 1 2 2 2 2
#Question 2
sensor_matrix <- matrix(c(23.2, 12, 10, 15, 32, 55, 10, 4, 56, 45, 10, 30), nrow=3, byrow=T)
outliers <- c(10, 55)
update_sensor_readings <- function(mat, vec){
for (x in 1:dim(mat)[1]){
for (y in 1:dim(mat)[2]){
if(mat[x, y] %in% vec){
neighbors <- c()
if (x >1){neighbors <- append(neighbors, mat[x-1, y])}
if (x < nrow(mat)){neighbors <- append(neighbors, mat[x+1, y])}
if (y >1){neighbors <- append(neighbors, mat[x, y-1])}
if (y <1){neighbors <- append(neighbors, mat[x, y+1])}
1
if(length(neighbors) > 0){
mat[x,y] <- mean(neighbors)
}
}
}
}
print(mat)
}
update_sensor_readings(sensor_matrix, outliers)
#Question 3
num_list <- list(1, -2, 3, c(4, -3), -5, 6, c("text", "dot", "period"), -8, 9)
classify_numbers <- function(inputlist){
PositiveEven <- 0
PositiveOdd <- 0
NegativeEven <- 0
NegativeOdd <- 0
for(i in inputlist){
if(is.numeric(i)){
if(length(i) > 1){
for(x in i){
if(x > 0 & x %% 2 == 0){PositiveEven <- PositiveEven + 1}
else if(x > 0 & x %% 2 != 0){PositiveOdd <- PositiveOdd + 1}
else if(x < 0 & x %% 2 == 0){NegativeEven <- NegativeEven + 1}
else if(x < 0 & x %% 2 != 0){NegativeOdd <- NegativeOdd + 1}
}
}
else{
if(i > 0 & i %% 2 == 0){PositiveEven <- PositiveEven + 1}
else if(i > 0 & i %% 2 != 0){PositiveOdd <- PositiveOdd + 1}
else if(i < 0 & i %% 2 == 0){NegativeEven <- NegativeEven + 1}
else if(i < 0 & i %% 2 != 0){NegativeOdd <- NegativeOdd + 1}
}
}
else if(is.vector(i)){
for (j in i){
if(is.numeric(j)){
if(j > 0 & j %% 2 == 0){PositiveEven <- PositiveEven + 1}
else if(j > 0 & j %% 2 != 0){PositiveOdd <- PositiveOdd + 1}
else if(j < 0 & j %% 2 == 0){NegativeEven <- NegativeEven + 1}
else if(j < 0 & j %% 2 != 0){NegativeOdd <- NegativeOdd + 1}
}
}
}
}
results <- list(PositiveEven, PositiveOdd, NegativeEven, NegativeOdd)
print(results)
2
}
classify_numbers(num_list)
## [[1]]
## [1] 2
##
## [[2]]
## [1] 3
##
## [[3]]
## [1] 2
##
## [[4]]
## [1] 2
#Question 4
##Problem 2 #Setup
#Question 1
## [1] 10
## 30%
## 62.7
3
quantile(diamonds$depth, c(0.175, 0.825))
## 17.5% 82.5%
## 60.6 62.8
Revenue <- 0
Sold <- 0
PriceTag <- c(diamonds$price)
while(Revenue <= 200000){
Revenue <- Revenue + max(PriceTag)
PriceTag <- PriceTag[-max(PriceTag)]
Sold <- Sold+1
}
print(Sold)
## [1] 11
To be in the middle 65% of the data, the depth must be between 60.6 and 62.8. The minimum
diamonds sold is 11 for that revenue
#Question 2
## [1] "J"
median(diamonds$price[diamonds$cut == "Fair"])
## [1] 3282
median(diamonds$price[diamonds$cut == "Ideal"])
## [1] 1810
mean(diamonds$price[diamonds$cut == "Fair"])
## [1] 4358.758
mean(diamonds$price[diamonds$cut == "Ideal"])
## [1] 3457.542
4
## [1] 11586.5
The median price for Fair diamonds is significantly higher than that for Ideal ones, and the mean
shows a similar pattern, though less significant. You didn’t specify what variable to find the IQR
of so I am assuming price?
#Question 3
5000
0
Diamond Clarity
#Question 4
boxplot(diamonds$price ~ clarity_groups, xlab= "Diamond Clarity", ylab= "Price", main = "Diamond Price b
5
Diamond Price by Quality
15000
10000
Price
5000
0
Diamond Clarity
6
Diamond Price by Quality and Size
15000
10000
Price
5000
0
mean(diamonds$price[clarity_groups=="Bad"])
## [1] 3862.761
mean(diamonds$price[clarity_groups=="Medium"])
## [1] 4487.283
mean(diamonds$price[clarity_groups=="Good"])
## [1] 3525.903
## [1] 3858.022
mean(diamonds$price[clarity_groups=="Medium"])
## [1] NA
7
mean(diamonds$price[clarity_groups=="Good"])
## [1] 3526.666
The mean is highest in the Medium quality group, oddly enough. The other two are relatively
close together. After filtering, the mean “medium” value has been changed to NA. This means
that by removing everything with table value under 45, we have inadvertently removed every
single Medium clarity diamond from the data set despite barely impacting the “good” and “bad”
categories.