###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_generator = function(df, i, min, max){
sA = df[i, 1]
eA = df[i, 2] - 1
fA = df[i, 4]
rc = floor(runif(fA, min = sA, max = eA))
return (rc)
}
# random numbers between 15<= x < 20
r = random_generator(myData, 1, 15, 20 )
rec = r
# random numbers between 20 <= x < 25
r = random_generator(myData, 2, 20, 25)
rec = append(rec, r)
# random numbers between 25 <= x < 30
r = random_generator(myData, 3, 25, 30)
rec = append(rec, r)
# random numbers between 30 <= x < 35
r = random_generator(myData, 4, 30, 35)
rec = append(rec, r)
# random numbers between 35 <= x < 40
r = random_generator(myData, 5, 35, 40)
rec = append(rec, r)
# random numbers between 40 <= x < 45
r = random_generator(myData, 6, 40, 45)
rec = append(rec, r)
#
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=FALSE, main = "Mileage Economy Test", xlab="Average mpg", ylab="Frequency", col="blue")