Question: Raw CSV file provided for the following questions: https://raw.githubusercontent.com/pandahappy204/Sample/main/mydata.csv (Question 1) Prepare a table in RStudio showing each cutoff along with the following: 1) true
Raw CSV file provided for the following questions: https://raw.githubusercontent.com/pandahappy204/Sample/main/mydata.csv
(Question 1)
Prepare a table in RStudio showing each cutoff along with the following:
1) true positive rate (1-prop.adults),
2) false positive rate (1-prop.infants),
3) harvest proportion of the total population
Provide table and R code.
To calculate the total harvest proportions, you can use the 'count' approach, but ignoring TYPE; simply count the number of individuals (i.e. rows) with VOLUME greater than a given threshold and divide by the total number of individuals in our dataset.
Below Code Potentially Needed to solve for problem:
library(flux)
library(rockchalk)
library(car)
library(tidyverse)
mydata$VOLUME <- mydata$LENGTH * mydata$DIAM * mydata$HEIGHTmydata$RATIO <- mydata$SHUCK / mydata$VOLUME
mydata$L_RATIO <- log10(mydata$RATIO)
mydata$TYPE <- rockchalk::combineLevels(mydata$SEX, levs = c("F", "M"), "ADULT")
mydata <- mydata %>% mutate(L_SHUCK = log10(SHUCK), L_VOLUME = log10(VOLUME))
mature <- mydata$CLASS %in% c('A4', 'A5')
mydata$TYPE[mature] <- rockchalk::combineLevels(
mydata$TYPE[mature],
levs = c('I', 'ADULT'),
'ADULT'
)
model <- lm(L_SHUCK ~ L_VOLUME + CLASS + TYPE, data = mydata)summary(model)
idxi <- mydata$TYPE == "I"
idxa <- mydata$TYPE == "ADULT"
max.v <- max(mydata$VOLUME)
min.v <- min(mydata$VOLUME)
delta <- (max.v - min.v)/10000
prop.infants <- numeric(10000)
prop.adults <- numeric(10000)
volume.value <- numeric(10000)
total.infants <- sum(idxi)
total.adults <- sum(idxa)
for (k in 1:10000) {
value <- min.v + k*delta
volume.value[k] <- value
prop.infants[k] <- sum(mydata$VOLUME[idxi] <= value)/total.infants
prop.adults[k] <-sum(mydata$VOLUME[idxa] <= value)/total.adults
}
num_infants <- sum(prop_infants <= 0.5)
split_infants <- min_vol + (num_infants + 0.5) * delta
num_adults <- sum(prop_adults <= 0.5)
split_adults <- min_vol + (num_adults + 0.5) * delta
y_loess_a <- loess(1 - prop_adults ~ vol_value,
span = 0.25, family = c("symmetric"))
y_loess_i <- loess(1 - prop_infants ~ vol_value,
span = 0.25, family = c("symmetric"))
smooth_diff <- predict(y_loess_a) - predict(y_loess_i)
cut1 <- vol_value[which.max(smooth_diff)]
a1i <- with(mydata, max(VOLUME[CLASS == 'A1' & TYPE == 'I']))
cut2 <- first(vol_value[vol_value > a1i])
cut3 <- vol_value[which.min(abs(prop_adults + prop_infants - 1))]
cuts <- which(vol_value %in% c(cut1, cut2, cut3))
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
