The Dynaco Manufacturing Company produces a product on a series of five machines. The probability distribution for the number of machines that will breakdown in a 40-hour week is shown in Table 1. Every time a machine breaks down the time required to fix it is shown in Table 2. Simulate the machine breakdowns for 20 weeks.
| Table 1 | Table 2 | |||
|---|---|---|---|---|
| Breakdowns per week | Probability | Repair time (hrs) | Probability | |
| 0 | 0.1 | 1 | 0.3 | |
| 1 | 0.1 | 2 | 0.5 | |
| 2 | 0.2 | 3 | 0.2 | |
| 3 | 0.25 | |||
| 4 | 0.3 | |||
| 5 | 0.05 |
set.seed(123)
BreakDist <- c(0:5)
BreakProbDist <- c(0.1, 0.1, 0.2, 0.25, 0.3, 0.05)
avgBDsTheoretical <- crossprod(BreakDist,BreakProbDist)
RepairTime <- c(1:3)
RepairProbDist <- c(0.3, 0.5, 0.2)
NumWeeks <- 20
df31 <- data.frame(NumBDs = sample(BreakDist,NumWeeks,replace=TRUE, prob = BreakProbDist))
#Creates a dataframe for the 20 weeks, sampling from the BreakDist distribution to simulate the number of breakdowns each week based on the probabilities.
avgBDsSim <- mean(df31$NumBDs)
# calculating the average number of breakdowns per week
Using a while loop to simulate the repair time for each breakdown. The loop subtracts 1 from the number of breakdowns (i), each time sampling from the RepairTime distribution to add to a cumulative repair time (x)
After calculating the total repair hours (x) for the week, it updates the numHRs column with the total hours and the ttlCost column with the total cost, calculated as the total hours times the cost per hour (CostPrHour).
CostPrHour <- 50
df31$numHRs <- c(1:NumWeeks)
df31$ttlCost <- c(1:NumWeeks)
for (r in 1:nrow(df31)) {
i = df31[r,1]
x = 0
while (i>0) {
x = x + sample(RepairTime,1,replace=TRUE, prob = RepairProbDist)
i = i-1
}
df31[r,2] <- x
df31[r,3] <- x*CostPrHour
}
Provides a statistical summary of the dataframe df31, which now includes the number of breakdowns, total repair hours, and total repair costs for each of the 20 weeks.
| NumBDs | numHRs | ttlCost | |
|---|---|---|---|
| Min. :0.00 | Min. : 0.0 | Min. : 0 | |
| 1st Qu.:1.75 | 1st Qu.: 3.0 | 1st Qu.:150 | |
| Median :3.00 | Median : 5.5 | Median :275 | |
| Mean :2.75 | Mean : 5.1 | Mean :255 | |
| 3rd Qu.:4.00 | 3rd Qu.: 7.0 | 3rd Qu.:350 | |
| Max. :5.00 | Max. :10.0 | Max. :500 |
Created a histogram of the total repair costs for the 20 weeks,
visualizing the distribution of costs incurred from repairs.
Build a model to track travel expenses for a business person over a 25-day month. On each working day, the person either makes a trip with P(trip) = 40% or doesn’t with P(no trip) = 60%. On days a trip occurs, there is uncertainty about the:
Setting up variables given in the problem
MonthDays <- 25
TripProb <- 0.40
Distance <- c(200,350,500)
DistProb <- c(0.5,0.3,0.2)
SpeedMean <- 50 # normally distributed
SpeedSD <- 10
GasPriceMax <- 5.50
GasPriceMin <- 4.25
ttlCost <- c()
ttlHours <- c()
The loop runs 500 iterations to simulate different monthly outcomes.
for (j in 1:500) {
NumTrips <- rbinom(1, MonthDays,TripProb)
NumMilesPerTrip <- sample(Distance,NumTrips,replace=TRUE, prob = DistProb)
#For each iteration, the number of trips in the month is simulated using a binomial distribution, based on the number of days and the trip probability.
GasPrice <- runif(NumTrips, min = GasPriceMin, max = GasPriceMax)
#Simulates the distance of each trip, sampled from the Distance options with probabilities DistProb.
speed <- rnorm(NumTrips, mean = SpeedMean, sd = SpeedSD)
MPG <- c()
for (i in 1:NumTrips) {
MPG <- append(MPG, -0.011*speed[i]^2 + 1.3399*speed[i] - 4.0893)
}
#Calculates miles per gallon (MPG) for each trip based on a quadratic equation related to speed, reflecting how fuel efficiency changes with speed.
Cost <- c()
Hours <- c()
for (i in 1:NumTrips) {
Cost[i] <- NumMilesPerTrip[i]/MPG[i]*GasPrice[i]
Hours[i] <- NumMilesPerTrip[i]/speed[i]
}
ttlCost[j] <- round(sum(Cost), digits = 2)
ttlHours[j]<- round(sum(Hours), digits = 1)
}
For each trip, it calculates cost & time taken for for each trip. Then it summarizes total cost and hours for the month.
Provide statistical summaries for the total cost across all simulations.
| Min. | 1st Qu. | Median | Mean | 3rd Qu. | Max |
|---|---|---|---|---|---|
| 130.0 | 345.8 | 436.5 | 434.4 | 513.1 | 787.5 |
Provide statistical summaries for total hours across all simulations.
| Min. | 1st Qu. | Median | Mean | 3rd QU. | Max |
|---|---|---|---|---|---|
| 19.40 | 50.17 | 64.30 | 63.43 | 75.22 | 124.20 |
Creates a scatter plot of total hours versus total cost for the 500
simulated months. Adds a linear regression line to the plot, indicating
the trend between total hours spent on trips and the total cost, helping
to visualize the relationship between these variables.