Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

STAT2102 Midterm1 RMD

Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

STAT2102 Midterm

Nathan Shurts

2024-10-07

##Problem 1
#Question 1

Required_Attempts <- c()


charge_devices <- function(required_charge, attempted_rounds){
i <- 0
Charge <- 0
x <- 0
while(Charge < required_charge){
Charge <- Charge + sample(c(5, 10, 15, 20, 25, 30), 1, prob= c(0.2, 0.3, 0.2, 0.15, 0.1, 0.05)) + sa
i <- i+1
if (Charge >= required_charge){
Required_Attempts <<- append(Required_Attempts, i)
x <- x+1
if (x == attempted_rounds){
break
}
Charge <- 0
i <- 0
}
}
}
charge_devices(40, 5)
print(Required_Attempts)

## [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)

## [,1] [,2] [,3] [,4]


## [1,] 23.2 12.00000 11.00000 15
## [2,] 32.0 29.66667 16.88889 4
## [3,] 56.0 45.00000 30.94444 30

#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

test_scores_variation <- function(mean_score, score_sd){


differences <- c()
for (n in 1:50){
differences <- append(differences, mean_score - mean(rnorm(n = n, mean = mean_score, sd = score_sd))
}
print(differences)
}
test_scores_variation(75, 10)

## [1] -1.546317169 10.679001789 -0.740833258 -0.988850149 -4.031714484


## [6] 0.740931432 -1.360862752 -0.003030032 1.901733195 -2.716897940
## [11] -1.232455420 -0.127825328 3.870989265 1.056967175 1.426792413
## [16] 3.416675544 3.299442467 3.688230702 1.671130073 -0.219539527
## [21] -0.642882791 1.526326607 -1.683949713 -1.896446053 0.794566952
## [26] 1.355306982 1.924688300 -0.064927867 1.015511459 -1.095307528
## [31] 2.727362890 0.506102305 -1.649923952 1.526967596 0.644896883
## [36] -2.573901040 -1.905911217 1.204351979 1.288639801 -0.724569990
## [41] -0.284058093 -2.975247234 0.830253680 -0.162690521 1.018475414
## [46] 1.064099003 -0.728983803 -0.059506098 1.846615729 0.431813461

##Problem 2 #Setup

diamonds <- read.csv(file= "diamonds.csv")

#Question 1

length(diamonds[diamonds$clarity == "VS2" & diamonds$color == "D" & diamonds$x < 10, ])

## [1] 10

quantile(diamonds$price[diamonds$cut=="Premium"], 0.3) - quantile(diamonds$price[diamonds$cut=="Very Goo

## 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

variability <- tapply(diamonds$carat, diamonds$color, sd)


print(names(variability)[which.max(variability)])

## [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

data <- diamonds[


(diamonds$cut %in% c("Good", "Fair")) &
(diamonds$color %in% c("J", "D")) &
(diamonds$clarity == "IF"),
]
IQR(data$price)

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

clarity_groups <- factor(


ifelse(diamonds$clarity %in% c("SI1", "I1", "IF"), "Bad",
ifelse(diamonds$clarity %in% c("SI2", "VS1"), "Medium", "Good")), levels= c("Bad", "Medium", "G
)
diamonds$clarity_groups <- clarity_groups

barplot(table(clarity_groups), xlab= "Diamond Clarity", ylab="Count", main= "Diamond Inventory by Clarit

Diamond Inventory by Clarity


10000 15000 20000
Count

5000
0

BAD MEDIUM GOOD

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

Bad Medium Good

Diamond Clarity

carat_class <- factor(


ifelse(diamonds$carat < median(diamonds$carat), "SMALL", "LARGE"),
levels = c("SMALL", "LARGE"))

boxplot(diamonds$price ~ clarity_groups:carat_class, xlab= "Diamond Clarity and Size", ylab= "Price", ma

6
Diamond Price by Quality and Size
15000
10000
Price

5000
0

Bad Small Good Small Medium Large

Diamond Clarity and Size

> There are lots of outliers for each category


#Question 5

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

diamonds <- diamonds[diamonds$table >= 45, ]


mean(diamonds$price[clarity_groups=="Bad"])

## [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.

You might also like