I have neither given nor received unauthorized assistance on this test or assignment.

George Petkau

2.

# Compute the first 100 Fibonacci numbers starting with 1
fib = 1:100
fib[1] = 1
fib[2] = 1

for (i in 3:100) {
  fib[i] = fib[i - 1] + fib[i - 2]
}

# Compute the ratios x_{n+1} / x_n for n = 1 to 99
ratios = fib[2:100] / fib[1:99]


# Plot the ratios
plot(1:99, ratios, type = "l", col = "blue",
     main = "Ratio of Fibonacci Numbers",
     xlab = "n", ylab = "ratio")

When we look at the ratio of consecutive numbers in the Fibonacci Sequence, we can clearly see that the ratio evens out around 1.6.

3.

drops = 1:10000
circle = 0
square = 0
r = 0.5
list_x = list()
list_y = list()

for (i in drops){
  x = runif(n=1, min=0, max=1)
  list_x = append(list_x, x)
  y = runif(n=1, min=0, max=1)
  list_y = append(list_y, y)
  square = square + 1
  check = x**2 + y**2 <= r**2
  if (check == TRUE){
    circle = circle + 1
  }
  
}

pie_est = (4*circle) / square
pie_est
## [1] 0.7936
par(pty="s")
plot(x = list_x, y = list_y, pch = 16, cex = .3, col = "red", xlab = "x values", ylab = "y values", main = "Points used to Estimate Pie")
symbols(x = 0.5, y = 0.5, circles = 0.5, add = TRUE, inches = FALSE, fg = "blue", lwd = 2)

4.

library(babynames)

babynames
## # A tibble: 1,924,665 × 5
##     year sex   name          n   prop
##    <dbl> <chr> <chr>     <int>  <dbl>
##  1  1880 F     Mary       7065 0.0724
##  2  1880 F     Anna       2604 0.0267
##  3  1880 F     Emma       2003 0.0205
##  4  1880 F     Elizabeth  1939 0.0199
##  5  1880 F     Minnie     1746 0.0179
##  6  1880 F     Margaret   1578 0.0162
##  7  1880 F     Ida        1472 0.0151
##  8  1880 F     Alice      1414 0.0145
##  9  1880 F     Bertha     1320 0.0135
## 10  1880 F     Sarah      1288 0.0132
## # ℹ 1,924,655 more rows
babynames %>% group_by(name)
## # A tibble: 1,924,665 × 5
## # Groups:   name [97,310]
##     year sex   name          n   prop
##    <dbl> <chr> <chr>     <int>  <dbl>
##  1  1880 F     Mary       7065 0.0724
##  2  1880 F     Anna       2604 0.0267
##  3  1880 F     Emma       2003 0.0205
##  4  1880 F     Elizabeth  1939 0.0199
##  5  1880 F     Minnie     1746 0.0179
##  6  1880 F     Margaret   1578 0.0162
##  7  1880 F     Ida        1472 0.0151
##  8  1880 F     Alice      1414 0.0145
##  9  1880 F     Bertha     1320 0.0135
## 10  1880 F     Sarah      1288 0.0132
## # ℹ 1,924,655 more rows
babynames %>% ungroup()
## # A tibble: 1,924,665 × 5
##     year sex   name          n   prop
##    <dbl> <chr> <chr>     <int>  <dbl>
##  1  1880 F     Mary       7065 0.0724
##  2  1880 F     Anna       2604 0.0267
##  3  1880 F     Emma       2003 0.0205
##  4  1880 F     Elizabeth  1939 0.0199
##  5  1880 F     Minnie     1746 0.0179
##  6  1880 F     Margaret   1578 0.0162
##  7  1880 F     Ida        1472 0.0151
##  8  1880 F     Alice      1414 0.0145
##  9  1880 F     Bertha     1320 0.0135
## 10  1880 F     Sarah      1288 0.0132
## # ℹ 1,924,655 more rows
  1. The “babynames” data set contains 1,924,665 rows and 5 columns. It includes the year, the sex, the name, the count of how many babies were named that name in that year, and the proportion of all names that that name makes up.

  2. There appears to be 97,310 unique names. This is less than the total number of rows because the data set looks at names every year, so, a name will appear each year that it is used.

# Most popular male names
male_1900 <- babynames[babynames$year == 1900 & babynames$sex == "M", ]
male_1925 <- babynames[babynames$year == 1925 & babynames$sex == "M", ]
male_1950 <- babynames[babynames$year == 1950 & babynames$sex == "M", ]
male_1975 <- babynames[babynames$year == 1975 & babynames$sex == "M", ]
male_2000 <- babynames[babynames$year == 2000 & babynames$sex == "M", ]

top_male_1900 <- male_1900[which.max(male_1900$n), ]
top_male_1925 <- male_1925[which.max(male_1925$n), ]
top_male_1950 <- male_1950[which.max(male_1950$n), ]
top_male_1975 <- male_1975[which.max(male_1975$n), ]
top_male_2000 <- male_2000[which.max(male_2000$n), ]

print(c(top_male_1900, top_male_1925, top_male_1950, top_male_1975, top_male_2000))
## $year
## [1] 1900
## 
## $sex
## [1] "M"
## 
## $name
## [1] "John"
## 
## $n
## [1] 9829
## 
## $prop
## [1] 0.06062307
## 
## $year
## [1] 1925
## 
## $sex
## [1] "M"
## 
## $name
## [1] "Robert"
## 
## $n
## [1] 60896
## 
## $prop
## [1] 0.05288659
## 
## $year
## [1] 1950
## 
## $sex
## [1] "M"
## 
## $name
## [1] "James"
## 
## $n
## [1] 86239
## 
## $prop
## [1] 0.04740837
## 
## $year
## [1] 1975
## 
## $sex
## [1] "M"
## 
## $name
## [1] "Michael"
## 
## $n
## [1] 68454
## 
## $prop
## [1] 0.0421767
## 
## $year
## [1] 2000
## 
## $sex
## [1] "M"
## 
## $name
## [1] "Jacob"
## 
## $n
## [1] 34471
## 
## $prop
## [1] 0.01651392
# Most popular female names
female_2010 <- babynames[babynames$year == 2010 & babynames$sex == "F", ]
female_2011 <- babynames[babynames$year == 2011 & babynames$sex == "F", ]
female_2012 <- babynames[babynames$year == 2012 & babynames$sex == "F", ]
female_2013 <- babynames[babynames$year == 2013 & babynames$sex == "F", ]
female_2014 <- babynames[babynames$year == 2014 & babynames$sex == "F", ]

top_female_2010 <- female_2010[which.max(female_2010$n), ]
top_female_2011 <- female_2011[which.max(female_2011$n), ]
top_female_2012 <- female_2012[which.max(female_2012$n), ]
top_female_2013 <- female_2013[which.max(female_2013$n), ]
top_female_2014 <- female_2014[which.max(female_2014$n), ]

print(c(top_female_2010, top_female_2011, top_female_2012, top_female_2013, top_female_2014))
## $year
## [1] 2010
## 
## $sex
## [1] "F"
## 
## $name
## [1] "Isabella"
## 
## $n
## [1] 22905
## 
## $prop
## [1] 0.01169646
## 
## $year
## [1] 2011
## 
## $sex
## [1] "F"
## 
## $name
## [1] "Sophia"
## 
## $n
## [1] 21837
## 
## $prop
## [1] 0.011285
## 
## $year
## [1] 2012
## 
## $sex
## [1] "F"
## 
## $name
## [1] "Sophia"
## 
## $n
## [1] 22304
## 
## $prop
## [1] 0.01151924
## 
## $year
## [1] 2013
## 
## $sex
## [1] "F"
## 
## $name
## [1] "Sophia"
## 
## $n
## [1] 21213
## 
## $prop
## [1] 0.01102629
## 
## $year
## [1] 2014
## 
## $sex
## [1] "F"
## 
## $name
## [1] "Emma"
## 
## $n
## [1] 20924
## 
## $prop
## [1] 0.01072117
  1. Most popular male names by year:

1900: John

1925: Robert

1950: James

1975: Michael

2000: Jacob

Most popular female names by year:

2010: Isabella

2011: Sophia

2012: Sophia

2013: Sophia

2014: Emma

# Get all male baby names
males <- babynames[babynames$sex == "M", ]

# Add up all the babies with the same male name
male_totals <- aggregate(n ~ name, data = males, sum)

# Sort male names from biggest number to smallest
male_totals_sorted <- male_totals[order(-male_totals$n), ]

# Take the first 10 rows (the top 10 male names)
top_10_males <- male_totals_sorted[1:10, ]

# Show the top 10 male names
print(top_10_males)
##          name       n
## 17080   James 5150472
## 19152    John 5115466
## 32106  Robert 4814815
## 26918 Michael 4350824
## 39123 William 4102604
## 8678    David 3611329
## 19566  Joseph 2603445
## 31855 Richard 2563082
## 6314  Charles 2386048
## 36587  Thomas 2304948
# Do the same for females
females <- babynames[babynames$sex == "F", ]
female_totals <- aggregate(n ~ name, data = females, sum)
female_totals_sorted <- female_totals[order(-female_totals$n), ]
top_10_females <- female_totals_sorted[1:10, ]

# Show the top 10 female names
print(top_10_females)
##            name       n
## 41738      Mary 4123200
## 18700 Elizabeth 1629679
## 48449  Patricia 1571692
## 27718  Jennifer 1466281
## 37748     Linda 1452249
## 7732    Barbara 1434060
## 40814  Margaret 1246649
## 58205     Susan 1121440
## 17656   Dorothy 1107096
## 53264     Sarah 1073895
  1. The top 10 most popular male names:
  1. James
  2. John
  3. Robert
  4. Michael
  5. William
  6. David
  7. Joseph
  8. Richard
  9. Charles
  10. Thomas

The top 10 most popular female names:

  1. Mary
  2. Elizabeth
  3. Patricia
  4. Jennifer
  5. Linda
  6. Barbara
  7. Margaret
  8. Susan
  9. Dorothy
  10. Sarah