Homework Q #5 Exercise 3_9

###A researcher conducts a mileage economy test involving 80 cars. The frequency distribution describing average miles per gallon (mpg) appears in the following table. ###a-1. Construct the relative frequency distribution. (Round “Relative Frequency” to 4 decimal places.)

avgmpg = c("[15, 20)", "[20, 25)", "[25, 30)", "[30, 35)", "[35, 40)", "[40, 45)")
freq = c(15, 30, 15, 10, 7, 3)
start = c(15, 20, 25, 30, 35, 40)
end = c(20, 25, 30, 35, 40, 45)
total = sum(freq)
relfreq = round(freq/total, 4)
myData = data.frame(StartBin = start, EndBin = end, AverageMpg = avgmpg, Frequency=freq, RelFreq = relfreq)
myData
##   StartBin EndBin AverageMpg Frequency RelFreq
## 1       15     20   [15, 20)        15  0.1875
## 2       20     25   [20, 25)        30  0.3750
## 3       25     30   [25, 30)        15  0.1875
## 4       30     35   [30, 35)        10  0.1250
## 5       35     40   [35, 40)         7  0.0875
## 6       40     45   [40, 45)         3  0.0375

###a-2. What proportion of the cars got at least 20 mpg but less than 25 mpg? (Round your answer to 4 decimal places.)

## [1] "The proportion of the cars got at least 20 mpg but less than 25 mpg is 0.375."

###a-3. What proportion of the cars got less than 35 mpg? (Round your answer to 4 decimal places.)

myData1 = myData[which(myData$EndBin < 35),]
n = myData1[1, 5]
sprintf("The proportion of the cars got at less 35 mpg is %s.", n)
## [1] "The proportion of the cars got at less 35 mpg is 0.1875."

###a-4. What proportion of the cars got 35 mpg or more? (Round your answer to 4 decimal places.)

myData1 = myData[which(myData$EndBin >= 35),]
n = myData1[1, 5]
sprintf("The proportion of the cars got at 35 mpg or more is %s.", n)
## [1] "The proportion of the cars got at 35 mpg or more is 0.125."

###b. Which statement is correct regarding the shape of the distribution using a histogram?

# random numbers between 15<= x < 20
sA = myData[1, 1]
eA = myData[1, 2]
fA = myData[1, 4]
rec = floor(runif(fA, min = sA, max = eA))
# random numbers between 20 <= x < 25
sA = myData[2, 1]
eA = myData[2, 2]
fA = myData[2, 4]
rec = append(rec, floor(runif(fA, min = sA, max = eA)))
# random numbers between 25 <= x < 30
sA = myData[3, 1]
eA = myData[3, 2]
fA = myData[3, 4]
rec = append(rec, floor(runif(fA, min = sA, max = eA)))
# random numbers between 30 <= x < 35
sA = myData[4, 1]
eA = myData[4, 2]
fA = myData[4, 4]
rec = append(rec, floor(runif(fA, min = sA, max = eA)))
# random numbers between 35 <= x < 40
sA = myData[5, 1]
eA = myData[5, 2]
fA = myData[5, 4]
rec = append(rec, floor(runif(fA, min = sA, max = eA)))
# random numbers between 40 <= x < 45
sA = myData[6, 1]
eA = myData[6, 2]
fA = myData[6, 4]
rec = append(rec, floor(runif(fA, min = sA, max = eA)))

myData2 = data.frame(Frequency = rec)
intervals = seq(15, 45, by=5)
value.cut = cut(myData2$Frequency, intervals, left= TRUE, right=FALSE)
value.freq = table(value.cut)

hist(myData2$Frequency, breaks = intervals, right=TRUE, main = "Mileage Economy Test", xlab="Average mpg", ylab="Frequency", col="blue")