R Statistics Assignment: Data Analysis Tasks
R Statistics Assignment: Data Analysis Tasks
A linear regression model helps predict mpg based on wt and hp. The coefficients indicate the model's estimate of mpg changes: a negative coefficient for wt (e.g., -3.917) suggests higher weight decreases mpg, whereas a positive hp coefficient would imply the opposite. The R-squared and Adjusted R-squared values measure fit quality; high values (close to 1) indicate strong predictivity, assessing the model's explanatory power and efficiency for the Mtcar's dataset .
With base R, filter the dataset using subset() for cars having 'mpg' greater than 20 and 'hp' less than 100 like this: subset(mtcars, mpg > 20 & hp < 100). Displaying the first five rows of this filtered data shows the specific cars meeting these criteria, illustrating basic conditional operations in R .
To export the 'mtcars' dataset to a .csv file, you can use the write.csv() function with the dataset and specify the file path. For example: write.csv(mtcars, 'mtcars_export.csv'). To read it back into R as a new object called cars_data, you use the read.csv() function: cars_data <- read.csv('mtcars_export.csv').
Using the dplyr package, you can calculate these averages by grouping the dataset by the 'cyl' column and then summarizing the mean values. Specifically, you would use: mtcars %>% group_by(cyl) %>% summarize(avg_hp = mean(hp), avg_wt = mean(wt)).
Predict new mpg values using the predict() function with the model object and new data: predict(lm_model, newdata=mtcars[1:5,]). This compares predicted mpg against actual values for the first five cars. Differences illustrate the model's precision and areas where it diverges, providing insights into model tracking and potential improvements .
Using ggplot2, a boxplot to explore 'mpg' across 'cyl' can be created with ggplot(mtcars, aes(x=factor(cyl), y=mpg)) + geom_boxplot(). This displays variations in mpg distribution for each cylinder category, facilitating an understanding of how cylinder numbers impact fuel efficiency .
Pairwise scatterplot matrices, created with pairs(mtcars[,c('mpg', 'hp', 'wt', 'qsec')]), allow visualization of potential relationships between multiple variables simultaneously. They help identify correlations, outliers, and patterns within the data, offering deeper insights in exploratory analysis of the 'mtcars' dataset .
To add an 'efficiency' column defined as mpg / wt, use either base R or dplyr. In base R, achieve this with mtcars$efficiency <- mtcars$mpg / mtcars$wt. Alternatively, with dplyr's mutate(), use: mtcars <- mtcars %>% mutate(efficiency = mpg / wt). Both methods update the dataset with a column illustrating fuel efficiency relative to weight .
In base R, you can sort the 'mtcars' dataset by 'mpg' in descending order using the order() function: mtcars <- mtcars[order(-mtcars$mpg),]. With dplyr, use arrange(mtcars, desc(mpg)) for a more readable syntax. Both reorder the dataset to highlight cars with the highest mpg values first .
Base R provides fundamental functions for data manipulation and visualization, such as subsetting data and basic plotting. Tidyverse, including tools like dplyr and ggplot2, offers more readable syntax and powerful operations for both tasks. Integrating both helps achieve efficiency and enhances clarity and functionality of R scripts by combining the simplicity of base R with the complexity and aesthetics of tidyverse .