Diving into R Analysis
Explore the comprehensive guide on analyzing fast food data with R. Gain valuable insights into the nutritional aspects of fast food while leveraging our expertise to help with your R assignment. Uncover hidden trends and make informed decisions with our data-driven approach. Whether you're a student looking for assistance with your R-based assignments or a data enthusiast seeking to dive into the world of fast food nutrition, this resource provides a solid foundation for your journey. Join us in exploring the fascinating realm of data analysis and its real-world applications.
Block 1: Load Libraries and Data
```R
library(tidyverse)
library(openintro)
library(lm.beta)
fast_food <- openintro::fastfood
```
This block loads three R packages (tidyverse, openintro, and lm.beta) and assigns the "fastfood" dataset from the "openintro" package to the variable "fast_food."
Block 2: Calculate Correlation and Store in Q1
```R
Q1 <- round(cor(na.omit(fast_food[fast_food$restaurant %in% c('Sonic', 'Subway', 'Taco Bell'), c('calories', 'total_fat', 'sugar', 'calcium')])), 2) %>% as.data.frame()
print(Q1)
```
In this block, the code calculates the correlation between selected variables (calories, total_fat, sugar, and calcium) for specific restaurants (Sonic, Subway, Taco Bell) and stores the result in the data frame Q1. It then prints the Q1 data frame.
Block 3: Convert "restaurant" to a Factor
```R
fast_food$restaurant <- as.factor(fast_food$restaurant)
```
This block converts the "restaurant" column in the "fast_food" dataset to a factor, which is useful for modeling and analysis.
Block 4: Data Filtering and Logistic Regression (Q2 and Q3)
```R
fast_food_ms <- fast_food %>% filter(restaurant == "Mcdonalds" | restaurant == "Subway")
fast_food_ms$restaurant = as.integer(fast_food_ms$restaurant == "Mcdonalds")
lm_fit1 <- glm(restaurant ~ calories + sodium + protein, fast_food_ms, family = "binomial")
Q2 <- lm_fit1$coefficients
Q2 <- round(Q2, 2)
lm_fit2 <- glm(restaurant ~ calories + protein, fast_food_ms, family = "binomial")
AIC_model_fit = AIC(lm_fit1, lm_fit2)
Q3 = min(AIC_model_fit$AIC)
Q3 = round(Q3, 2)
```
This block first filters the "fast_food" dataset to select only rows where the restaurant is "Mcdonalds" or "Subway." It then transforms the "restaurant" column into binary values (1 for Mcdonalds, 0 for Subway) and fits two logistic regression models. Q2 stores the coefficients of the first logistic regression model, and Q3 stores the minimum AIC value from the model comparison.
Block 5: Linear Regression and Store in Q4
```R
lm_fit3 <- lm(calories ~ sat_fat + fiber + sugar, fast_food)
std_coeff = lm.beta(lm_fit3)
Q4 <- coef(lm_fit3)[2]
Q4 <- round(Q4, 2)
```
In this block, a linear regression model is fitted with "calories" as the response variable and "sat_fat," "fiber," and "sugar" as predictor variables. Q4 stores the coefficient of "sat_fat" from the model.
Block 6: Data Transformation and Filtering (Q5 and Q5_data)
```R
transformed_data <- fast_food %>% group_by(restaurant) %>% summarise("n" = n()) %>% filter(n >= 50 & n <= 60)
transformed_data
Q5_data <- fast_food %>% filter(restaurant %in% transformed_data$restaurant)
```
This block creates a summary dataset "transformed_data" that groups the "fast_food" dataset by restaurant, calculates the count of observations ("n"), and then filters for restaurants with counts between 50 and 60. "Q5_data" is created by filtering the "fast_food" dataset to include only the selected restaurants.
Block 7: Linear Regression and Summary Statistics (Q5)
```R
lm_fit4 <- lm(total_fat ~ cholesterol + total_carb + vit_a + restaurant, Q5_data)
summary(lm_fit4)
lm_fit4_new <- lm(total_fat ~ cholesterol + total_carb + restaurant, Q5_data)
summary(lm_fit4_new)
std_coef <- lm.beta(lm_fit4_new)
Q5 <- round(std_coef$standardized.coefficients[2], 2)
Q5
```
In this block, two linear regression models are fitted to the "Q5_data" dataset. The first model includes "vit_a" as a predictor variable, while the second model does not. Summary statistics are printed for both models. Q5 stores the standardized coefficient of the "restaurant" variable from the second model.
Block 8: Additional Data Manipulation and Calculations
```R
fastfood <- openintro::fastfood
Q1 <- fastfood %>% filter(restaurant %in% c("Chick Fil-A", "Burger King")) %>% group_by(item) %>%
summarise("sum" = sum(calories)) %>% arrange(desc(sum)) %>% head(1)
Q1 <- Q1$item[1]
Q2 <- fastfood %>% filter(restaurant %in% c("Subway")) %>%
group_by(restaurant) %>% summarize("mean_sugar" = round(mean(sugar), 2))
Q2 <- data.frame(Q2)
Q2 <- Q2$mean_sugar[1]
Q3 <- fastfood %>% filter(restaurant %in% c("Taco Bell")) %>%
summarize(mean = mean(calories))
Q3 <- round(Q3$mean[1], 2)
fastfood <- fastfood %>% mutate("fatXSugar" = total_fat * sugar)
Q4 = fastfood %>% group_by(restaurant, item) %>% summarise("fatXSugar" = mean(fatXSugar)) %>%
arrange(desc(fatXSugar)) %>% head(3)
Temp_Data <- fastfood %>% group_by(restaurant) %>% summarise("average" = mean(sat_fat))
newdata <- subset(Temp_Data, average > 10)
Q5 <- nrow(newdata)
```
In this block, various data manipulations are performed, including filtering for specific restaurants, calculating statistics, and creating new variables. Q1, Q2, Q3, Q4, and Q5 store the results of these calculations.
Conclusion
In conclusion, our journey through the analysis of fast food data using R has unveiled a world of nutritional insights. By meticulously examining correlations, applying regression models, and filtering data, we've gained a deeper understanding of the factors that contribute to the nutritional profiles of popular fast food restaurants. This exploration has demonstrated the power of data analysis in shedding light on the impact of dietary choices, offering valuable knowledge for both data enthusiasts and those interested in making informed decisions about their fast food consumption.