Lab-1-2611

pdf

School

Worcester Polytechnic Institute *

*We aren’t endorsed by this school

Course

2611

Subject

Mathematics

Date

May 27, 2024

Type

pdf

Pages

4

Uploaded by MinisterInternetSalmon14

Report
MA2611 LAB 1 Part 1: Introducing R 1. Sources Here we use R (Statistical software). • R itself can be found at: http://cran.r-project.org • I also highly recommend the RStudio front end. It makes developing R code much easier. It can be found at: http://www.rstudio.com • Note, RStudio requires that you have R itself already installed (so you have to access both of the web pages above). Good place to start: A Step-by-Step Function Guide to Data Analysis by Richard Cotton O'Reilly Media, September 2013 available for free from the library. A comprehensive R tutorial: http://cran.r-project.org/doc/manuals/R-intro.html . 2. Some Basics To quit R session, use the following command (or simply close the R console window): > q() Here are some illustrations of R’s interactive nature (using simply the command window): > 5 #you type in a 5 at the prompt and nothing after that. [1] 5 Here note that ‘#’ sign is considered a comment and will not be processed. 2.1 Number Operations > 5 + 4 ### adding two numbers [1] 9 > 5^3 ### will compute 5^3 [1] 125 > pi ### This is a default object. [1] 3.141593 > 10-4 # Subtraction [1] 6 > 3*4 # Multiplication [1] 12 > 5/3 # Division [1] 1.666667
2.2 Vectors To create a vector of numbers or characters, “c( )” command can be used. > c(1,3,4,7) # To create a vector of numbers: [1] 1 3 4 7 > c("Andrew","Daniel","Rachel","Sarah") # To create a vector of characters: [1] "Andrew" "Daniel" "Rachel" "Sarah" If elements in the vector have a systematic pattern, the following command can also be used. > seq(from=1,to=9,by=1) # values from 1 to 9 increased by 1. [1] 1 2 3 4 5 6 7 8 9 > rep(4, 5) #repeat 4 five times. [1] 4 4 4 4 4 Here note that we can give a name to any object we create as follows. > x <- c(1,3,4,7) # ”< - ” can also be used instead use “=” sign > y <- c("Andrew","Daniel","Rachel","Sarah") > temp = c(1,3,4,7) The following commands can be used with the named objects. > ls() # to show the list of all the named objects created. [1] "temp" "x" "y" > rm(temp) # To remove object “tempt” from the list (directory). > ls() [1] "x" "y" > x # to view the vector x: [1] 1 3 4 7 > y [1] "Andrew" "Daniel" "Rachel" "Sarah" > x # to view the vector x: [1] 1 3 4 7 > y [1] "Andrew" "Daniel" "Rachel" "Sarah" > x[2] # View the second element of x: [1] 3 > y[c(1,3)] # View the first and third elements of y, note that “y[1,3]” would fail here. [1] "Andrew" "Rachel
The following commands can also be used to descriptive measures of a vector of values. > min(x) # maximum of x [1] 1 > max(x) # minimum of x [1] 7 > mean(x) # mean of x [1] 3.75 > median(x) # median of x [1] 3.5 > length(x) #number of elements in x [1] 4 Logical operations can also be used with vectors. > a<-c(9,4,3,2,5,7,8,6,1) > b<-1:9 > c<-seq(from=1,to=9,by=1) > d=c("Y", "N", "N", "Y", "N", "Y", "N", "N", "Y") > a==b # checking element by element [1] FALSE FALSE TRUE FALSE TRUE FALSE FALSE FALSE FALSE > c==b [1] TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE TRUE > sum(a==b) #number of elements of a and b coincide [1] 2 > sum(a>5) #number of elements in a > 5. [1] 4 > a>5 # check wether elemnts in a greater than 5 or not. [1] TRUE FALSE FALSE FALSE FALSE TRUE TRUE TRUE FALSE > which(a>5) # positions of the elements >5. [1] 1 6 7 8 > a[which(a>5)] # elements in a >5 in order. [1] 9 7 8 6 > Prop=sum(d=="Y")/length(d) # calculating proportion of Ys in d. > Prop [1] 0.4444444 3. R Functions There are many built-in functions in R. Here we use the function “sample()”. For the detail description type > ?sample # this will show the description of the function sample() can generate a random sample from a finite population (i.e., from a finite vector). > sample(c(1,2,3,4,5),3) # Sample of three from c(1,2,3,4,5) [1] 1 5 4
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
To see the randomness, try the first command again. Note that the sample you are getting is different from time to time. > sample(x,1) ### remember x=c(1,3,4,7) [1] 7 > sample(y,2) ### remember y=c("Andrew","Daniel","Rachel","Sarah") [1] "Sarah" "Rachel" Part 2 Lab Questions Simple Random Samples: Name Anna Sarah Luke Viren Alex Alton Ani Mila ID 1 2 3 4 5 6 7 8 Wants an ipad air No No Yes No Yes Yes No Yes 1. Draw a simple random sample of size 4 using the above function, sample(). Which IDs were chosen for your sample? What are the names of the individuals in your sample? 2. Calculate the sample proportion, i.e., the percentage of Yes in your sample, of who wants to purchase the iPad air. 3. Draw another sample of size 4 and calculate the sample proportion. Do you think the sample proportion will remain the same? Simple Random Samples vs Stratified Samples: Name Charles Heath Daria Jack Joe Nick Spencer Ryan Nigel Rayna ID 1 2 3 4 5 6 7 8 9 10 Major E E A E E E V E A V E = Engineer, A = Architect, V = Violinist 4. Draw a simple random sample of size 4 using “sample()”. Is violin major represented in your sample? 5. Name two advantages of stratified sampling compared to simple random samples. Explain an advantage of stratified sampling in terms of the violinists. 6. Now stratify by major. Draw a simple random sample of 3 engineers from the 6 engineers, 1 architect from the 2 architects, and 1 violin major from the 2 violinists. What are the names of the 5 people in your sample?