Lab4.knit
pdf
keyboard_arrow_up
School
Syracuse University *
*We aren’t endorsed by this school
Course
687
Subject
Statistics
Date
Feb 20, 2024
Type
Pages
4
Uploaded by DeanTigerMaster997
9/21/23, 9:36 PM
Lab4.knit
file:///C:/Users/morea/Downloads/Lab4.html
1/4
Intro to Data Science - Lab 4
Copyright 2022, Jeffrey Stanton and Jeffrey Saltz Please do not post online.
Week 4 - Sampling
# Enter your name here: Adesh More
Please include nice comments.
Instructions:
Run the necessary code on your own instance of R-Studio.
Attribution statement: (choose only one and delete the rest)
# 1. I did this lab assignment by myself, with help from the book and the professor.
A key focus of this week is how to make inferences about populations based on samples. The essential logic lies
in comparing a single instance of a statistic such as a sample mean to a distribution of such values. The
comparison can lead to one of two conclusions the sample statistic is either extreme or not extreme. But what are
the thresholds for making this kind of judgment call (i.e., whether a value is extreme or not)? This activity explores
that question.
The problem is this:
You receive a sample containing the ages of 30 students. You are wondering whether this
sample is a group of undergraduates
(mean age = 20 years) or graduates
(mean age = 25 years).
To answer this question, you must compare the mean of the sample you receive to a distribution of means from
the population. The following fragment of R code begins the solution:
set.seed(2)
sampleSize <- 30
studentPop <- rnorm(20000,mean=20,sd=3)
undergrads <- sample(studentPop,size=sampleSize,replace=TRUE)
grads <- rnorm(sampleSize,mean=25,sd=3)
if (runif(1)>0.5) { testSample <- grads } else { testSample <- undergrads }
mean(testSample)
After you run this code, the variable ** testSample ** will contain either a sample of undergrads or a sample of
grads. The line before last flips a coin by generating one value from a uniform distribution
(by default the
distribution covers 0 to 1) and comparing it to 0.5. The question you must answer with additional code is: Which is
it, grad or undergrad?
Here are the steps that will help you finish the job:
9/21/23, 9:36 PM
Lab4.knit
file:///C:/Users/morea/Downloads/Lab4.html
2/4
1. Copy the code above and annotate it with line-by-line commentary. In other words, you must explain what
each of the seven lines of code above actually do! You will have to lookup the meaning of some commands.
set.seed(2) # maintains consistency, sets random seed to 2
sampleSize <- 30 #setting sample size
studentPop <- rnorm(20000,mean=20,sd=3) # using rnorm() to generate 20k values
undergrads <- sample(studentPop,size=sampleSize,replace=TRUE) # creating undergrads
grads <- rnorm(sampleSize,mean=25,sd=3) # creating grads
if
(runif(1)>0.5) { testSample <- grads } else
{ testSample <- undergrads } # generating random values in testSample using runif())
mean(testSample)
## [1] 24.89729
2. Generate 10 samples from the ** undergrads ** dataset.
undergrads_10 <- sample(undergrads, size= 10, replace= FALSE)
undergrads_10
## [1] 14.13022 25.57921 14.32067 25.92212 13.99717 21.48056 24.82276 21.07116
## [9] 18.73168 19.87687
3. Generate 10 new samples and take the mean of that sample
undergrads_10_1 <- sample(undergrads, size= 10, replace= FALSE)
mean(undergrads_10_1)
## [1] 18.26193
4. Repeat this process 3 times (i.e., generate a sample and take the mean 3 times, using the replicate
function).
undergrads_replicated <- replicate(3,sample(undergrads, size= 10, replace= FALSE))
mean(undergrads_replicated)
## [1] 19.68058
5. Generate a list of sample means from the population called ** undergrads ** How many sample means
should you generate? Really, you can create any number that you want hundreds, thousands, whatever but
I suggest for ease of inspection that you generate just 100 means. That is a pretty small number, but it
makes it easy to think about percentiles
and ranks.
undergrads_100 <- replicate(100, mean(sample(undergrads, size= 10, replace= FALSE)))
mean(undergrads_100)
## [1] 19.60209
9/21/23, 9:36 PM
Lab4.knit
file:///C:/Users/morea/Downloads/Lab4.html
3/4
6. Once you have your list of sample means generated from undergrads
, the trick is to compare
mean(testSample)
to that list of sample means and see where it falls. Is it in the middle of the pack? Far
out toward one end?
Here is one hint that will help you: In chapter 7, the quantile()
command is used to generate percentiles
based on thresholds of 2.5% and 97.5%. Those are the thresholds we want, and the quantile()
command
will help you create them.
# The mean I got is close to the original sample mean.
percentile <- quantile( undergrads_100, c(0.025, 0.975))
7. Your code should have a print()
statement that should say either, Sample mean is extreme, or, Sample
mean is not extreme.
if
(mean(undergrads_100) >= percentile[1] || mean(undergrads_100) <= percentile[2]) {
print("Sample Mean is not extreme.")
} else
{
print("Sample Mean is extreme.")
}
## [1] "Sample Mean is not extreme."
8. Add a comment stating if you think the testSample
are undergrad students. Explain why or why not.
# testSample are not undergrad students as average is around 25.
9. Repeat the same analysis to see if the testSample
are grad students.
grad_means <- replicate(100, mean(sample(grads, size=10, replace=FALSE)))
mean(grad_means)
## [1] 24.98488
quant <- quantile(grad_means, probs = c(0.025, 0.975))
quant
## 2.5% 97.5% ## 23.40300 26.23425
mean(testSample)
## [1] 24.89729
if
((mean(testSample) < quant[1]) || (mean(testSample) > quant[2])) {
print("mean was extreme")
} else
{ print ("mean was not extreme")
}
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
- Access to all documents
- Unlimited textbook solutions
- 24/7 expert homework help
9/21/23, 9:36 PM
Lab4.knit
file:///C:/Users/morea/Downloads/Lab4.html
4/4
## [1] "mean was not extreme"
# It is grad students as mean is close to the same.
Related Documents
Related Questions
Only need help with 1 & 2. Must be calculated in Excel.
Thanks
arrow_forward
5lqloGU/edit
Sora
Zearn
Papa's Burgeria - Pl..
Apps
September equinox..
Request edit access
14
YoU'Re having a BBQ this
weekend, and you'Re IN chaRge
of hamburger Meat. Right Now
there is a sale where you cAN
buy 5 pounds of Meat 'for $10.
At this Rate, hoW Much meat
Could you buy fOR $30?
arrow_forward
Please help!
arrow_forward
Please share an excel screen on how to input and calculate the data for #1 only.
Thank you
arrow_forward
Use a graphing utility to evaluate nPr
50P4
arrow_forward
Help asap
arrow_forward
primaverahs.strongmind.com
After participating in a track meet, athletes were given the option to drink a sports drink or water. The results showed that 85 athletes chose a sports drink, 62 athletes
chose water, and 14 athletes chose not to drink anything. Of those that chose water or a sports drink, 27 athletes choose to drink both.
Use the Venn diagram to help answer the question.
Sports Drink
Water
© 2020 StrongMind. Created using GeoGebra.
(?
How many athletes did not choose water?
72
99
58
85
Next >
O Chat
+
O O
arrow_forward
This tab
arrow_forward
Predict the value of the painting in 2025
arrow_forward
explain how to iton. graphing calculator
arrow_forward
CAN YOU PLEASE WRITE OUT THE WORK, not type it ASAP. thanks
arrow_forward
Evaluate fi fyrydady
arrow_forward
But how will I draw it month by month similar to the picture? Thanks.
arrow_forward
please i need help :)
arrow_forward
O asd-sp-01
+
ntFunctions/Interface/acellus_engine.html?ClassID=785229986
M Compose Mail - kla.
P PicsArt / Editor
Dayforce
Intro to Pythagorean Theorem
Acellus
Find the value of x.
60
36
X = [?]
Enter the number that belongs in
the green box.
Enter
Copyright © 2003 - 2021 Acellus Corporation. AI Rights Reserved.
arrow_forward
The number of countries a world traveler has visited within the past 3 years is an example of a
arrow_forward
* Question Completion Status:
QUESTION 1
Arnold purchased a $1,300 set of golf clubs on a nine-month layaway plan and had to pay a monthly payment of $158.89. What is the fee charged for the layaway plan?
For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac).
BIUS
Paragraph
Arial
14px
A
arrow_forward
SEE MORE QUESTIONS
Recommended textbooks for you
![Text book image](https://www.bartleby.com/isbn_cover_images/9780998625713/9780998625713_smallCoverImage.jpg)
Elementary Algebra
Algebra
ISBN:9780998625713
Author:Lynn Marecek, MaryAnne Anthony-Smith
Publisher:OpenStax - Rice University
![Text book image](https://www.bartleby.com/isbn_cover_images/9780547587776/9780547587776_smallCoverImage.jpg)
Holt Mcdougal Larson Pre-algebra: Student Edition...
Algebra
ISBN:9780547587776
Author:HOLT MCDOUGAL
Publisher:HOLT MCDOUGAL
![Text book image](https://www.bartleby.com/isbn_cover_images/9781305115545/9781305115545_smallCoverImage.gif)
College Algebra
Algebra
ISBN:9781305115545
Author:James Stewart, Lothar Redlin, Saleem Watson
Publisher:Cengage Learning
Related Questions
- Only need help with 1 & 2. Must be calculated in Excel. Thanksarrow_forward5lqloGU/edit Sora Zearn Papa's Burgeria - Pl.. Apps September equinox.. Request edit access 14 YoU'Re having a BBQ this weekend, and you'Re IN chaRge of hamburger Meat. Right Now there is a sale where you cAN buy 5 pounds of Meat 'for $10. At this Rate, hoW Much meat Could you buy fOR $30?arrow_forwardPlease help!arrow_forward
- primaverahs.strongmind.com After participating in a track meet, athletes were given the option to drink a sports drink or water. The results showed that 85 athletes chose a sports drink, 62 athletes chose water, and 14 athletes chose not to drink anything. Of those that chose water or a sports drink, 27 athletes choose to drink both. Use the Venn diagram to help answer the question. Sports Drink Water © 2020 StrongMind. Created using GeoGebra. (? How many athletes did not choose water? 72 99 58 85 Next > O Chat + O Oarrow_forwardThis tabarrow_forwardPredict the value of the painting in 2025arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Elementary AlgebraAlgebraISBN:9780998625713Author:Lynn Marecek, MaryAnne Anthony-SmithPublisher:OpenStax - Rice UniversityHolt Mcdougal Larson Pre-algebra: Student Edition...AlgebraISBN:9780547587776Author:HOLT MCDOUGALPublisher:HOLT MCDOUGALCollege AlgebraAlgebraISBN:9781305115545Author:James Stewart, Lothar Redlin, Saleem WatsonPublisher:Cengage Learning
![Text book image](https://www.bartleby.com/isbn_cover_images/9780998625713/9780998625713_smallCoverImage.jpg)
Elementary Algebra
Algebra
ISBN:9780998625713
Author:Lynn Marecek, MaryAnne Anthony-Smith
Publisher:OpenStax - Rice University
![Text book image](https://www.bartleby.com/isbn_cover_images/9780547587776/9780547587776_smallCoverImage.jpg)
Holt Mcdougal Larson Pre-algebra: Student Edition...
Algebra
ISBN:9780547587776
Author:HOLT MCDOUGAL
Publisher:HOLT MCDOUGAL
![Text book image](https://www.bartleby.com/isbn_cover_images/9781305115545/9781305115545_smallCoverImage.gif)
College Algebra
Algebra
ISBN:9781305115545
Author:James Stewart, Lothar Redlin, Saleem Watson
Publisher:Cengage Learning