Lab assignment 1

pdf

School

University of California, Los Angeles *

*We aren’t endorsed by this school

Course

10

Subject

Mathematics

Date

Apr 3, 2024

Type

pdf

Pages

10

Uploaded by SargentCloverMonkey159

Report
Lab 1 Sec)on 1: R and RStudio Basics 1. Vectors a) Create a vector named ‘height’ that contains the heights, in inches, of yourself and two of your friends or family members’. Print the contents of this vector. > height=c (50,60,70) > print(height) [1] 50 60 70 b) Create a vector named ‘shows’ that contains the names of your favorite TV or NeAlix shows. Print the contents of this vector. > shows = c("blood hounds", "nevertheless", "hometown chachacha") > print(shows) [1] "blood hounds" "nevertheless" "hometown chachacha" c) Try typing cbind (height, shows). What did this command do? > cbind(names, heights) names heights [1,] "Leslie" "65" [2,] "Ron" "63" [3,] "Jerry" "71" [4,] "April" "69" This command combines height and shows and treats them as columns. d) Create an object'new' with cbind (height, shows). What is the class of the object 'new' in R? Hint: try the class () funcQon > new = cbind(height, shows) > class(new) [1] "matrix" "array" e. what is the dimension of 'new' in R? Hint: Try the dim() funcQon > dim(new) [1] 3 2
2) Downloading Data a) Download the data set births2023.csv from the Bruinlearn and upload it into RStudio. Name the data frame NCbirths NCbirths = read.csv(file.choose()) head(NCbirths) b) Demonstrate that you have been successful by typing head(NCbirths) and copying and pasQng the output into your word processing document. head(NCbirths) Gender Premie weight Apgar1 Fage Mage Feduc Meduc TotPreg Visits 1 Female No 118 8 30 20 12 11 3 13 2 Male No 106 8 23 26 12 11 2 12 3 Female No 130 8 21 19 12 12 1 11 4 Male No 112 9 34 30 14 14 3 7 5 Female No 102 9 25 26 12 14 2 14 6 Female No 134 9 20 19 12 13 1 11 Marital Racemom Racedad Hispmom Hispdad Gained Habit 1 Unmarried Black Unknown NotHisp Unknown 44 Smoker 2 Unmarried Black Black NotHisp NotHisp 30 Smoker 3 Unmarried White White NotHisp NotHisp 32 NonSmoker 4 Married White White NotHisp NotHisp 30 NonSmoker 5 Married Black Black NotHisp NotHisp 53 NonSmoker 6 Married White White NotHisp NotHisp 68 NonSmoker MomPriorCond BirthDef DelivComp BirthComp 1 None None At Least One None 2 None None None At Least One 3 None None None None 4 At Least One None At Least One None 5 None None None None 6 None None None None
> 3) Load the maps package a) Install the maps package. Verify its installaQon by typing find.package("maps") and include the output in your answer. > find.package("maps") [1] “/Library/Frameworks/R.framework/Versions/4.3-arm64/Resources/library/maps” b) Type library(maps) to load up the package. Type map("state") and include the plot output in your answer. > library(maps) > map("state") 4) Perform Vector Opera)ons
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
a) Extract the weight variable as a vector from the data fram the data frame by typing weights <- NCbirths$weight > weights <- NCbirths$weight b) What units do you think the weights are in? The weights are in ounces. c) Create a new vector named weights_in_pounds which are the weights of the babies in pounds > weights_in_pounds = weights/16 d) Demonstrate your success by typing weights_in_pounds[1:10] and including the output in your word processing document. This will print the first 10 weights of the babies in the dataset. > weights_in_pounds[1:10] [1] 7.3750 6.6250 8.1250 7.0000 6.3750 8.3750 7.2500 8.3750 7.3125 [10] 3.8125 Sec)on 2: Summarizing Data 1) What is the mean and standard deviaQon of the fathers' age in the dataset? hint: use variable "fage" > father_age= NCbirths$Fage > mean(father_age) [1] 30.22596 > sd(father_age) [1] 6.04709 2) How many of mothers in the sample smoke? #hint: use tally funcQon > tally(NCbirths$Habit, format = "count") X
NonSmoker Smoker 1697 175 175 mothers smoke > tally(NCbirths$Habit, format = "percent") X NonSmoker Smoker 90.651709 9.348291 9.3% of the mothers in the sample smoke 3) According to the Centers for Disease Control, nearly 17 of every 100 U.S. females aged 18 years or older (19.3%) smoked cigareses in 2023. How far off is the percentage you found in 2 from the CDC’s report in 2023. > 17 - 9.348291 [1] 7.651709 Sec)on 3: Visualizing Data (one quan)ta)ve variable) 1) Produce a dot plot of the 'weights in pounds. Describe what you find from the plot. library(mosaic) dotPlot(weights_in_pounds, pch = 2)
The plot is negaQvely skewed to the leu and is unimodal. There are no outliers apparent in the model. 2) Produce three different histograms of the weights in pounds. Use 5 bins, 20 bins, and 100 bins. Which histogram seems to give the best visualizaQon and why? histogram(weights_in_pounds, nint= 5)
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
histogram(weights_in_pounds, nint= 20) histogram(weights_in_pounds, nint= 100) The best visualizaQon appears to be the histogram that has 20 bins as it has not too lisle or much informaQon. The histogram with 5 bins has too lisle informaQon packed together, whereas the histogram with 100 bins has too much informaQon packed together.
3) We can use the syntax boxplot(vector1, vector2) to make a side by side box plot. Create a side by side boxplot of the mother’s educaQon (in years) and the father’s educaQon. Which gender tends to get more educated? > boxplot(NCbirths$Meduc, NCbirths$Feduc) Females tend to be more educated as they have more years of educaQon than the males in the side by side box plot. 4) Try typing histogram(~ weight | Premie, data = NCbirths, layout = c(1, 2)). Describe what this code does. Based on the graph, do you see any major differences between baby weights from premature babies vs. non-premature (normal) babies? histogram(~ weight | Premie, data = NCbirths, layout = c(1,2))
Sec)on 4: Visualizing Data 1) Consider the other categorical variables in this data. Which do you think will be associated with the mother’s smoking and why? Make a two-way Summary Table to check your hypothesis. Do you have evidence that this variable associated with smoking? Why? 2) Create a segmented bar chart of the two variables of your choice Hint: use the boxplot funcQon > barplot(tally(~Habit | DelivComp, data = NCbirths, format = "proporQon"))
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
Remember to submit this as a PDF file on Bruinlearn!