library(tidyverse)
library(openintro)download.file("http://www.openintro.org/stat/data/bdims.RData", destfile = "bdims.RData")
load("bdims.RData")head(bdims)## bia.di bii.di bit.di che.de che.di elb.di wri.di kne.di ank.di sho.gi che.gi
## 1 42.9 26.0 31.5 17.7 28.0 13.1 10.4 18.8 14.1 106.2 89.5
## 2 43.7 28.5 33.5 16.9 30.8 14.0 11.8 20.6 15.1 110.5 97.0
## 3 40.1 28.2 33.3 20.9 31.7 13.9 10.9 19.7 14.1 115.1 97.5
## 4 44.3 29.9 34.0 18.4 28.2 13.9 11.2 20.9 15.0 104.5 97.0
## 5 42.5 29.9 34.0 21.5 29.4 15.2 11.6 20.7 14.9 107.5 97.5
## 6 43.3 27.0 31.5 19.6 31.3 14.0 11.5 18.8 13.9 119.8 99.9
## wai.gi nav.gi hip.gi thi.gi bic.gi for.gi kne.gi cal.gi ank.gi wri.gi age
## 1 71.5 74.5 93.5 51.5 32.5 26.0 34.5 36.5 23.5 16.5 21
## 2 79.0 86.5 94.8 51.5 34.4 28.0 36.5 37.5 24.5 17.0 23
## 3 83.2 82.9 95.0 57.3 33.4 28.8 37.0 37.3 21.9 16.9 28
## 4 77.8 78.8 94.0 53.0 31.0 26.2 37.0 34.8 23.0 16.6 23
## 5 80.0 82.5 98.5 55.4 32.0 28.4 37.7 38.6 24.4 18.0 22
## 6 82.5 80.1 95.3 57.5 33.0 28.0 36.6 36.1 23.5 16.9 21
## wgt hgt sex
## 1 65.6 174.0 1
## 2 71.8 175.3 1
## 3 80.7 193.5 1
## 4 72.6 186.5 1
## 5 78.8 187.2 1
## 6 74.8 181.5 1
mdims <- subset(bdims, sex == 1)
fdims <- subset(bdims, sex == 0)par(mfrow=c(1,2))
hist(mdims$hgt, main="Men's Heights")
hist(fdims$hgt, main="Female's Height")fhgtmean <- mean(fdims$hgt)
fhgtsd <- sd(fdims$hgt)##Exercise 1, based on the above histograms I would say the men’s height has more of a bell shape to it. If you place the line of fit over both we will be able to see the shapes are different. The female’s heights have almost two other bars between 55-65 that almost is the same in frequency to the median.
hist(fdims$hgt, probability = TRUE)
x <- 140:190
y <- dnorm(x = x, mean = fhgtmean, sd = fhgtsd)
lines(x = x, y = y, col = "blue")##After plotting the density histogram with the first command, we create the x- and y-coordinates for the normal curve. We chose the x range as 140 to 190 in order to span the entire range of fheight. To create y, we use dnorm to calculate the density of each of those x-values in a distribution that is normal with mean fhgtmean and standard deviation fhgtsd. The final command draws a curve on the existing plot (the density histogram) by connecting each of the points specified by x and y. The argument col simply sets the color for the line to be drawn. If we left it out, the line would be drawn in black.
The top of the curve is cut off because the limits of the x- and y-axes are set to best fit the histogram. To adjust the y-axis you can add a third argument to the histogram function: ylim = c(0, 0.06).
##Exercise 2 ###Based on the this plot, does it appear that the data follow a nearly normal distribution? I would say based on the shape that the ine makes on the density curve it is fairly bell sahped, so yes a normal disturbution.
qqnorm(fdims$hgt)
qqline(fdims$hgt)sim_norm <- rnorm(n = length(fdims$hgt), mean = fhgtmean, sd = fhgtsd)##Exercise 3 ###Make a normal probability plot of sim_norm. Do all of the points fall on the line? How does this plot compare to the probability plot for the real data?
sim_norm## [1] 167.3042 149.8232 160.7664 171.5384 172.7535 148.9929 167.3027 170.1438
## [9] 175.9666 161.1290 166.5118 160.7759 167.5089 168.9799 164.1881 163.2182
## [17] 166.6645 154.5997 168.2971 164.5117 167.0961 156.9185 163.6923 167.8316
## [25] 171.9940 163.0958 166.2812 168.5366 172.1747 173.4949 174.8180 163.3878
## [33] 155.9138 157.5407 162.8727 169.4777 172.7624 168.4251 166.8452 165.3503
## [41] 174.6302 167.6903 166.8685 164.2256 160.2117 163.3249 166.5047 162.0860
## [49] 160.4796 162.2171 176.9949 174.7124 165.6855 172.0024 168.2385 166.7011
## [57] 169.2233 157.3169 159.5033 168.2029 176.4565 167.8529 170.2048 172.7682
## [65] 165.4850 159.8364 171.3073 174.2187 164.2338 162.1543 164.4424 166.3707
## [73] 159.4957 171.6485 159.8346 165.2115 166.3116 160.0754 158.5919 174.5449
## [81] 164.9070 168.7320 165.7184 159.1600 163.7787 156.6057 174.1519 165.1119
## [89] 159.5792 161.9270 160.6738 164.7173 172.8160 169.6733 157.8179 157.2383
## [97] 171.4071 167.7921 168.9741 155.9456 168.7541 167.4837 175.8250 163.3795
## [105] 162.2651 158.8516 156.8775 165.1748 169.7436 159.9419 163.4689 159.8835
## [113] 161.9681 175.7228 168.1967 154.2892 172.4930 149.0391 169.7214 179.1606
## [121] 154.4968 170.7614 162.6608 169.1381 174.0496 175.6304 156.8020 155.6088
## [129] 160.6426 172.6472 159.8911 165.7339 165.2553 163.6559 168.4357 164.2373
## [137] 165.8351 160.7553 162.1215 175.5891 169.0057 176.9710 162.2162 171.3439
## [145] 167.0774 172.2131 170.5332 162.1493 163.2389 150.2261 166.4800 166.6098
## [153] 171.6474 172.4030 171.5098 163.8101 166.6509 155.7010 152.6756 154.8699
## [161] 166.5756 165.7903 170.6097 170.4691 161.5856 162.5522 158.3434 167.1055
## [169] 178.6434 164.4288 170.5261 162.2805 170.7902 165.3988 173.8171 156.8089
## [177] 149.4553 164.5903 165.7906 157.9662 167.6268 168.4274 167.0339 175.4355
## [185] 169.9197 172.9294 169.9822 162.0891 160.0390 170.8564 161.6992 166.0154
## [193] 162.7558 169.1229 163.7280 171.2314 161.2023 162.6859 164.9762 170.5257
## [201] 157.9952 169.3557 152.5194 167.2629 176.5169 160.9650 163.4333 159.4277
## [209] 163.1347 157.9508 179.7345 163.9116 167.1409 163.7490 163.8208 159.5399
## [217] 172.9041 175.5147 161.5334 165.3252 153.7317 174.5531 160.7628 165.4132
## [225] 170.5038 175.0636 162.8902 154.1978 162.3306 165.4504 157.0733 164.0074
## [233] 171.2094 158.6968 154.0181 170.5244 158.1720 174.1663 167.0348 174.6420
## [241] 163.5951 169.4397 156.2751 158.0830 181.1686 175.4762 176.0841 159.6803
## [249] 166.0544 170.2109 171.4136 167.3007 175.4404 176.8746 158.6321 169.5423
## [257] 161.3499 167.1882 166.9886 174.8095
qqnorm(sim_norm)
qqline(sim_norm)qqnormsim(fdims$hgt)##Exercise 4 ###Does the normal probability plot for fdims$hgt look similar to the plots created for the simulated data? That is, do plots provide evidence that the female heights are nearly normal? ###The female height plot does look similar to the other 9 pots.
##Exercise 5 ###Using the same technique, determine whether or not female weights appear to come from a normal distribution.
hist(fdims$wgt, main="Female's Weight")##Just based off the histogram, Female’s weights seem to be a bit skewed to the right.
fwgtmean <- mean(fdims$wgt)
fwgtsd <- sd(fdims$wgt)hist(fdims$wgt, probability = TRUE)
x <- 35:95
y <- dnorm(x = x, mean = fwgtmean, sd = fwgtsd)
lines(x = x, y = y, col = "blue")##Now that the line is against the histogram, i would say it does look more bell shaped showing a normal disturbution.
qqnorm(fdims$wgt)
qqline(fdims$wgt)##Female weight and height is definetly not the same. Like I stated before I think the weight histogram shows some skewness and you can see that at the upper tail end, around 2-3 theorectical quantiles between 80-90 sample quantiles. the points do not touch the line and seem to curve a bit up away frpom the line.
1 - pnorm(q = 182, mean = fhgtmean, sd = fhgtsd)## [1] 0.004434387
sum(fdims$hgt > 182) / length(fdims$hgt)## [1] 0.003846154
##Exercise 6 ###Write out two probability questions that you would like to answer; one regarding female heights and one regarding female weights. Calculate the those probabilities using both the theoretical normal distribution as well as the empirical distribution (four probabilities in all). Which variable, height or weight, had a closer agreement between the two methods?
##What is the probality that a femae weighs more than 70 kg (154 ibs)? ##So we have the mean,60.600, and the SD, 9.616, we can find the Z score of this.
1 - pnorm(q = 70, mean = fwgtmean, sd = fwgtsd)## [1] 0.1641539
##This Z score is way bigger that the one we got for femae heights. Showing the weight is more towards not normal. We can also find the empiral probality.
sum(fdims$wgt > 70) / length(fdims$wgt)## [1] 0.1576923
##The theorectical is stil cose to the Z score, around 0.1, showing yes the weight is not a normal disturbution.