#> $vector
#> [1] 1.00000 2.00000 NA 5.50000 NA 7.00000 9.00000 35.00000
#> [9] 79.00000 88.00000 NA 3.14159
#>
#> $n
#> [1] 12
#>
#> $mean
#> [1] 25.51573
#>
#> $median
#> [1] 7
#>
#> $sd
#> [1] 34.5066
#>
#> $min
#> [1] 1
#>
#> $max
#> [1] 88
#>
#> $Q1
#> 25%
#> 3.14159
#>
#> $Q3
#> 75%
#> 35
#>
#> $sq.sum
#> [1] 15385.12
#>
#> $num.missing
#> [1] 3
Quiz 8 Instructions
Please complete the following questions and submit a file named Quiz8.R to Gradescope for autograding.
Remember:
- Do not use global paths in you script. Instead, use setwd() interactively in the console, but do not forget to remove or comment out this part of the code before you submit. The directory structure of your machine is not the same as the one on Gradescope’s virtual machines.
- Do not destroy or overwrite any variables in your program. I check them only after I have run your entire program from start to finish.
- Check to make sure you do not have any syntax errors. Code that doesn’t run will get a very bad grade.
- Make sure to name your submission Quiz8.R
Tip: before submitting, it might help to clear all the objects from your workspace, and then source your file before you submit it. This will often uncover bugs.
Question 1
[2pts] Create a function to show welcome information.
- Function Name:
welcome
- Function Input: Any string, like
John Doe
- Function Output: “Welcome to STAT 385,” and the input string, then the exclamation point. Example: “Welcome to STAT 385, John Doe!”
[Hint: You may use paste0()
function. Please make sure the number of spaces is correct, such as having a space after the comma.]
Question 2
[2pts] Create a function to calculate the \(t\)-statistic for a given random sample \(x\) and the specified mu
(defaulting to 0). Recall that \(t = \frac{\bar{x} - \mu}{s/\sqrt n}\), where \(\bar x\) is the sample mean, \(s\) is the sample standard deviation of \(x\), and \(n\) is the sample size.
- Function Name:
t.statistic
- Function Input:
x
,mu
. Please setmu = 0
as the default value. - Function Output: The t-statistic value.
Question 3
[2pts] Create a function to calculate the squared sum of a vector.
- Function Name:
squared_sum
- Function Input: Any numeric vector (may contains missing values).
- Function Output: The squared sum of the input vector excluding the missing values.
[Hint: You may use na.rm = TRUE
within sum()
to remove the missing values from calculation.]
Question 4
[4pts] Create a function to return the list of summary statistics: the input vector itself vector
, vector length n
, vector mean mean
, vector median value median
, standard deviation sd
, minimum min
, maximum max
, first quartile Q1
, third quartile Q3
, the squared sum sq.sum
(please use the function defined in Question 3), the number of missing values num.missing
.
- Function Name:
summary_stats
- Function Input: Any numeric vector (may contains missing values).
- Function Output: A list with values
vector
,n
,mean
,median
,sd
,min
,max
,Q1
,Q3
,sq.sum
,num.missing
. Please make sure the statistics follows the order specified.
After the function is defined, please use summary_stats(c(1, 2, NA, 5.5, NA, 7, 9, 35, 79, 88, NA, 3.14159))
to try the function. The result should look like the following: