library(psych)
library(tidyverse)
library(knitr)PHSP 502: Assignment 1
Load packages
It’s always a good idea to load the packages that you need for your analyses at the beginning of your syntax file. If you haven’t used a package before, you’ll need to install it first. You can do this using the install.packages() function. For example, running install.packages("psych") will install the psych package. After that, you’ll be able to load the psych package using the library() function.
Q1
A study is conducted to estimate the mean total cholesterol level in children who are 2 to 6 years of age. Researchers are able to recruit a sample of 9 children, and their total cholesterol levels (in mg/dL) were recorded as: 185 225 240 196 175 180 194 147 223.
- Compute the sample mean, standard deviation, and median.
- R users: use the following functions:
mean(),sd(), andmedian(). Hint: to see R’s instructions for how to use each function, type in a question mark followed by the function you want information on. For example, if you enter?mean(), it will show you the documentation for themean()function. To round your answers to a decimal place of your choosing, use theround()function. - Excel users: use the Data Analysis Tool
- R users: use the following functions:
- Compute the first and third quartiles.
- R users: use the
quantile()function. - Excel users: use the
quartilefunction
- R users: use the
R Code
# cholesterol data
cholesterol_levels <- c(185, 225, 240, 196, 175, 180, 194, 147, 223)
# a. Sample mean, standard deviation, and median
sample_mean_chol <- round(mean(cholesterol_levels), 4)
sample_std_dev_chol <- round(sd(cholesterol_levels), 4)
sample_median_chol <- median(cholesterol_levels) # median is a whole number, so rounding isn't necessary
# b. First and third quartiles
first_quartile_chol <- round(quantile(cholesterol_levels, 0.25), 4)
third_quartile_chol <- round(quantile(cholesterol_levels, 0.75), 4)Answers
| Metric | Value |
|---|---|
| Mean | 196.1111 |
| Standard Deviation | 29.0019 |
| Median | 194.0000 |
| First Quartile | 180.0000 |
| Third Quartile | 223.0000 |
Q2
Data were collected as part of a study of coffee consumption among graduate students. The following reflects cups per day consumed: 3 4 6 8 2 1 0 2.
- Compute the sample mean, standard deviation, and median.
- Compute the first and third quartiles.
R code
# coffee consumption data
coffee_consumption <- c(3, 4, 6, 8, 2, 1, 0, 2)
# a. Sample mean, standard deviation, and median
sample_mean_coffee <- round(mean(coffee_consumption), 4)
sample_std_dev_coffee <- round(sd(coffee_consumption), 4)
sample_median_coffee <- round(median(coffee_consumption), 4)
# b. First and third quartiles
first_quartile_coffee <- round(quantile(coffee_consumption, 0.25), 4)
third_quartile_coffee <- round(quantile(coffee_consumption, 0.75), 4)Answers
| Metric | Value |
|---|---|
| Mean | 3.2500 |
| Standard Deviation | 2.6592 |
| Median | 2.5000 |
| First Quartile | 1.7500 |
| Third Quartile | 4.5000 |
Q3
In a study of a new antihypertensive medication, SBPs, in mm Hg, are measured at baseline (or the start of the study, before any treatment is administered). The data are as follows: 120 112 138 145 135 150 145 163 148 128 143 156 160 142 150.
- Compute the sample mean, standard deviation, and median.
- Compute the first and third quartiles.
R code
# SBPs data (in mm Hg)
sbps_data <- c(120, 112, 138, 145, 135, 150, 145, 163, 148, 128, 143, 156, 160, 142, 150)
# a. Sample mean, standard deviation, and median
sample_mean_sbps <- round(mean(sbps_data), 4)
sample_std_dev_sbps <- round(sd(sbps_data), 4)
sample_median_sbps <- median(sbps_data) # median is a whole number, so rounding isn't necessary
# b. First and third quartiles
first_quartile_sbps <- round(quantile(sbps_data, 0.25), 4)
third_quartile_sbps <- round(quantile(sbps_data, 0.75), 4)Answers
| Metric | Value |
|---|---|
| Mean | 142.3333 |
| Standard Deviation | 14.1101 |
| Median | 145.0000 |
| First Quartile | 136.5000 |
| Third Quartile | 150.0000 |
Q4
The following are baby height measurements, in centimeters, for a sample of women participating in a study of pregnancy outcomes: 28 30 41 48 29 48 62 49 51 39.
- Compute summary statistics (i.e., the mean, median, standard deviation, kurtosis, skewness, range, minimum, maximum, and count).
- R users: use the
psych::describe()function. Hint: You’ll need to install thepsychpackage before you are able to use this function. The following code should do the trick:install.packages("psych"). You’ll then need to load the package:library(psych). - Excel users: use the Data Analysis Tool.
- R users: use the
R code
# baby height measurements
baby_heights <- c(28, 30, 41, 48, 29, 48, 62, 49, 51, 39)
# Use the describe function
summary_stats_baby <- describe(baby_heights)Answers
| mean | median | sd | skew | kurtosis | range | min | max | n | |
|---|---|---|---|---|---|---|---|---|---|
| X1 | 42.5 | 44.5 | 11.148 | 0.0814 | -1.3463 | 34 | 28 | 62 | 10 |