Exam 4, Spring 2024

ACCT 426 / BUDA 451

Your name: Ryan Riccardi

Before submitting, publish your results to rpubs RPubs URL: ________________________

Overview

You will use the starwars dataset included with dplyr.

See a description of the dataset below:

Starwars characters. A tibble with 87 rows and 14 variables. Source: R/data-starwars.R

name: Name of the character height: Height (cm) mass: Weight (kg) hair_color,skin_color,eye_color: Hair, skin, and eye colors birth_year: Year born (BBY = Before Battle of Yavin) sex: The biological sex of the character, namely male, female, hermaphroditic, or none (as in the case for Droids). gender: The gender role or gender identity of the character as determined by their personality or the way they were programmed (as in the case for Droids). homeworld: Name of homeworld species: Name of species films: List of films the character appeared in vehicles: List of vehicles the character has piloted starships: List of starships the character has piloted

Q1: Fix code (30%)

Fix the code below.

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Use the starwars tibble
# Create a summary of mass/height by species.

t_height_by_homeworld <- starwars %>% 
  select(name, height, mass, birth_year, species, sex, homeworld) %>% 
  mutate(is_human = ifelse(species == "Human", 1, 0 ),
         mass = mass * 2.2,
         height = height / 30.48 ) %>%  # convert to lbs and feet
  filter(!is.na(mass),
         !is.na(height),
         !is.na(homeworld),
         name != "Nal Hutta") %>% 
  group_by(species) %>% 
  summarise(n = n(), 
            height = mean(height),
            mass = mean(mass)) %>% 
  filter(n > 1)


ggplot(data = t_height_by_homeworld) +
  geom_point(mapping = aes(x = height, y = mass, color = species),
             size = 5) +
  labs(title = 'Species Height and Mass',
       x = 'Height (in ft)',
       y = 'Mass (in pounds)')

Q2: Chart of weight and gender (20%)

Create a chart to answer the question: “are Star Wars masculine characters heavier then feminine characters?”

Show the distribution of weight for each category. Place your answer as the title, and give the exact average value for each group. Do not show the ‘NA’ category.

Be sure to not have any warnings printed

t2 <- starwars %>% 
  select(sex,mass) %>% 
  filter(!is.na(sex),
         !is.na(mass),
         sex=='male'|sex=='female')

ggplot(t2) +
  aes(x = sex, y = mass) +
  geom_boxplot(fill = "light blue")+
  labs(title = 'Males are Heavier than Females',
       x = 'Sex',
       y = 'Mass')

Q3: Prediction from mass (20%)

Create a linear regression model that predicts height from mass. You should do any required clean-up in the data, as some of the rows will mess up your results. How good is your model?

  • The results of my model were pretty good. I managed to create a model with an adjusted R squared of 0.5486 and a p value well below 0.05. The variables I used were height, is_male, and home_tatooine. The model results paired with the variables used suggests that heavy males from Tatooine are more likely to be taller. It also means that these three variables are statistically significant and are a relatively strong predictor of height.
options(scipen = 99)

# Remove outlier: hist(t3$mass)
t3 <- starwars %>% 
  select(height,mass,birth_year,sex,species,homeworld) %>% 
  filter(!is.na(height),
         !is.na(mass),
         !is.na(birth_year),
         !is.na(sex),
         !is.na(species),) %>% 
  filter(mass<1000) %>% 
  mutate(is_male=ifelse(sex=='male',1,0),
         is_human=ifelse(species=='Human',1,0),
         home_tatooine=ifelse(homeworld=='Tatooine',1,0))

m1 <- lm(height~mass+home_tatooine+is_male,data=t3)

summary(m1)
## 
## Call:
## lm(formula = height ~ mass + home_tatooine + is_male, data = t3)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -45.040  -6.077   1.334  11.493  30.789 
## 
## Coefficients:
##               Estimate Std. Error t value       Pr(>|t|)    
## (Intercept)   108.6190    10.9586   9.912 0.000000000117 ***
## mass            1.0132     0.1816   5.580 0.000005703670 ***
## home_tatooine -17.4531     8.5243  -2.047         0.0501 .  
## is_male        -8.5460     8.9061  -0.960         0.3455    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 17.38 on 28 degrees of freedom
##   (3 observations deleted due to missingness)
## Multiple R-squared:  0.5923, Adjusted R-squared:  0.5486 
## F-statistic: 13.56 on 3 and 28 DF,  p-value: 0.00001196

Q4: Alternative prediction (30%)

Create a second linear regression model that predicts height from mass. This model should also see if people with a skin color of ‘fair’ are taller. It should also test to see if masculine characters are taller.

  • The results of model 2 are stronger than model 1. The model has an adjusted R squared of 0.6187 and a p value well below 0.05. Each variable is statistically significant and can be used to predict height. The variables is_masculine and fair_skin were used in this model. Based on the p value of the model, these variables can be seen as statistically significant and can be used in the final model. Model 2 shows that heavier, more masculine characters with fair skin are more likely to be taller.
options(scipen = 99)

t4 <- starwars %>% 
  select(mass,height,skin_color,gender) %>% 
  mutate(fair_skin=ifelse(skin_color=='fair',1,0),
         is_masculine=ifelse(gender=='masculine',1,0)) %>% 
  filter(!is.na(mass),
         !is.na(height),
         !is.na(skin_color),
         !is.na(gender),
         mass<1000)

m2 <- lm(height~mass+fair_skin+is_masculine,data=t4)

summary(m2)
## 
## Call:
## lm(formula = height ~ mass + fair_skin + is_masculine, data = t4)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -43.094 -12.406   1.804  10.260  52.770 
## 
## Coefficients:
##              Estimate Std. Error t value             Pr(>|t|)    
## (Intercept)  114.4017     9.6924  11.803 0.000000000000000337 ***
## mass           1.0451     0.1106   9.447 0.000000000000865873 ***
## fair_skin      8.6657     8.8375   0.981              0.33144    
## is_masculine -24.3356     8.8439  -2.752              0.00819 ** 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 22.72 on 51 degrees of freedom
## Multiple R-squared:  0.6399, Adjusted R-squared:  0.6187 
## F-statistic:  30.2 on 3 and 51 DF,  p-value: 0.00000000002288