Instructions

  1. This will be the typical format for all the homework assignments in this semester.
  2. Homework must be submitted as an .html file on Canvas and must show your code for credit. Feel free to reach out asap with any problems.
  3. Make sure you include author information (your name).
  4. Write your answers after each of the questions, wherever it says write your answer here.
  5. Remember, to run any \(\texttt{R}\) code, you must write the code within a \(\texttt{R}\) code chunk.



(Question 1)

Load the \(\texttt{splines}\) package in \(\texttt{R}\). Note, you DON’T need to install it; it usually comes with the basic \(\texttt{R}\). (5 points)

library(splines)



(Question 2)

Professor A asked the students to provide a rating between 0 (worst) to 10 (best) for her class. Following are the responses recorded by \(11\) students:

8, 7, 6.5, 7.5, 6.5, 10, 8.5, 8, 7, 10, 55
A. Calculate the mean, median, variance, and standard deviation of the Ratings. (10 points)
ratings= c(8,7,6.5,7.5,6.5,10,8.5,8,7,10,55)
mean(ratings)
## [1] 12.18182
median(ratings)
## [1] 8
var(ratings)
## [1] 203.1636
sd(ratings)
## [1] 14.25355
B. Calculate all the quartiles, the inter-quartile range (IQR), and range for the Ratings. (15 points)
min(ratings)
## [1] 6.5
max(ratings)
## [1] 55
quantile(ratings, 0.25)
## 25% 
##   7
quantile(ratings, 0.50)
## 50% 
##   8
quantile(ratings, 0.75)
##  75% 
## 9.25
IQR = IQR(ratings)
C. Calculate the \(45\)-th percentile of the ratings. (5 points)
quantile(ratings, 0.45)
##  45% 
## 7.75
D. Do you think the data you have above is correct? Is there any number that looks suspicous to you? Explain in words if you say “Yes”. (10 points)

No I believe there is a mistake in the data because there is an outlier of 55 skewing the data affecting the mean and variance.

E. Can you use a statistical tool (measures or diagram) to support your answer in part D. (10 points)
  • Hint: we plotted something similar in class (slides 02_Recap_part2).
boxplot(ratings)

F. Following your answer in Part D and E, how would you update your mean, median, variance, and IQR? (15 points)
quantile(ratings, c(0.25,0.50,0.75)) #3 quantiles
##  25%  50%  75% 
## 7.00 8.00 9.25
cat("Min=", min(ratings), "Max=", max(ratings)) #assigning min and max
## Min= 6.5 Max= 55
#finding lower and upper fences
cat("Upper Fence=",quantile(ratings,0.75) + 1.5 * IQR(ratings))
## Upper Fence= 12.625
cat("Lower Fence=", quantile(ratings,0.25) - 1.5 * IQR(ratings))
## Lower Fence= 3.625
#discard any values in the data outside the lower and upper fence since these are identified as outliers and recalculate mean, median, variance and IQR.
ratings= c(8,7,6.5,7.5,6.5,10,8.5,8,7,10)
mean(ratings)
## [1] 7.9
median(ratings)
## [1] 7.75
var(ratings)
## [1] 1.655556
IQR(ratings)
## [1] 1.375

Also explain what you are doing in this part here.

The lower and upper fence of the data set was calculated using the IQR and quantiles to determine outliers. After the fences are determined, “ratings” was redefined excluding the outlier (55) and the mean, median, var and IQR were recalculated.



(Question 3)

Let’s learn to load and analyze real data in \(\texttt{R}\). We will use the Body Fat Data, available here, courtesy of the Vanderbilt University Department of Biostatistics.

My following code downloads the data in \(\texttt{R}\) in a dataframe named \(\texttt{bodyfat_data}\). You need to be connected to internet for this.
bodyfat_data = read.csv(url("https://hbiostat.org/data/repo/bodyfat.csv"))
data("mtcars")
A. What are the variables (or columns) available to you in this data? (10 points)

Density, BodyFat, Age, Weight, Height, Neck, Chest, Abdomen, Hip, Thigh, Knee, Ankle, Biceps, Forearm, Wrist.

colnames(bodyfat_data)

  • Hint: To see the column names, the \(\texttt{R}\) syntax is: \(\texttt{colnames(<data>)}\)
B. Assume that the data on weights (column name Weight) is a sample from a normal distribution with unknown mean and variance.
i. What is a reasonable estimate for the population mean, median, and variance? (10 points)
Weight = bodyfat_data$Weight
mean(Weight)
## [1] 178.9244
median(Weight)
## [1] 176.5
var(Weight)
## [1] 863.7227
#sample mean and median are equal to population mean and median
ii. Construct a \(95\%\) confidence interval for it’s population mean. Report your answer as (lower, upper). (10 points)
t.test(Weight, conf.level = 0.95)$conf.int
## [1] 175.2783 182.5706
## attr(,"conf.level")
## [1] 0.95

95 confidence interval for population mean is (175.2783,182.5706)

iii. Construct a \(90\%\) confidence interval for it’s population mean. Report your answer as (lower, upper). (10 points)
t.test(Weight, conf.level = 0.90)$conf.int
## [1] 175.8679 181.9809
## attr(,"conf.level")
## [1] 0.9

90 confidence intveral for pop mean is (175.8679,181.9809)

iv. Test the null hypothesis that population mean for the \(\texttt{BodyFat}\) variable is 25. Take \(\alpha = 0.05\). Also write your null and alternate hypothesis. Clearly mention the test statistic and your conclusion from the test. (20 points).
  • \(H_0\): μ = 25
  • \(H_1\): μ ≠ 25
t.test(bodyfat_data, alternative= "two.sided", mu=25, conf.level = 0.95)
## 
##  One Sample t-test
## 
## data:  bodyfat_data
## t = 42.478, df = 3779, p-value < 2.2e-16
## alternative hypothesis: true mean is not equal to 25
## 95 percent confidence interval:
##  54.93212 57.82886
## sample estimates:
## mean of x 
##  56.38049

The test statistic is 42.478. The p-value found is 2.2e-16 and our significance level was 0.05, since the p-value is less than 0.05, we can reject the null hypothesis.

v. Test the null hypothesis that population mean for the \(\texttt{BodyFat}\) variable is less than \(17\). Take \(\alpha = 0.05\). Write your null and alternate hypothesis. Clearly mention the test statistic and your conclusion from the test. (20 points)
  • \(H_0\): mu greater than/equal to 17
  • \(H_1\): mu is less than 17
t.test(bodyfat_data,alternative="less", mu = 17,conf.level = 0.95)
## 
##  One Sample t-test
## 
## data:  bodyfat_data
## t = 53.307, df = 3779, p-value = 1
## alternative hypothesis: true mean is less than 17
## 95 percent confidence interval:
##      -Inf 57.59591
## sample estimates:
## mean of x 
##  56.38049

Test statistic= 52.307. The p-pvalue is 1 and our significance level was 0.05, since our p-value is greater than the significance level we fail to reject the null hypothesis since there is not enough statistical evidence to conclude.