This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ggplot(mtcars, aes(x = mpg, y = hp)) + | |
| geom_point(color = "#0099f9", size = 5) + | |
| geom_point(aes(size = qsec, color = cyl)) + | |
| labs( | |
| x = "Miles per Gallon", | |
| y = "Horse power" | |
| ) + | |
| theme( | |
| axis.title.x = element_text(color = "#0099f9", size = 16, face = "bold"), | |
| axis.title.y = element_text(color = "#0099f9", size = 16, face = "italic") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ggplot(mtcars, aes(x = mpg, y = hp)) + | |
| geom_point(aes(size = qsec, color = cyl)) + | |
| theme(legend.position = "top") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ggplot(mtcars, aes(x = mpg, y = hp)) + | |
| geom_point(size = 5, color = "#0099f9") + | |
| geom_rug() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(ggplot2) | |
| # Generate synthetic data with a clear linear relationship | |
| x <- seq(from = 1, to = 300) | |
| y <- rnorm(n = 300, mean = x + 2, sd = 25) | |
| # Convert to dataframe | |
| simple_lr_data <- data.frame(x, y) | |
| # Visualize as scatter plot |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Calculate coefficients | |
| b1 <- (sum((x - mean(x)) * (y - mean(y)))) / (sum((x - mean(x))^2)) | |
| b0 <- mean(y) - b1 * mean(x) | |
| # Define function for generating predictions | |
| simple_lr_predict <- function(x) { | |
| return(b0 + b1 * x) | |
| } | |
| # Apply simple_lr_predict() to input data |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(reshape) | |
| # Load in th dataset | |
| df <- read.csv("Fish.csv") | |
| # Remove target variable | |
| temp_df <- subset(df, select = -c(Weight)) | |
| melt_df <- melt(temp_df) | |
| # Draw boxplot |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(caTools) | |
| set.seed(42) | |
| # Train/Test split in 70:30 ratio | |
| sample_split <- sample.split(Y = df$Weight, SplitRatio = 0.7) | |
| train_set <- subset(x = df, sample_split == TRUE) | |
| test_set <- subset(x = df, sample_split == FALSE) | |
| # Fit the model and obtain summary | |
| model <- lm(Weight ~ ., data = train_set) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Get residuals | |
| lm_residuals <- as.data.frame(residuals(model)) | |
| # Visualize residuals | |
| ggplot(lm_residuals, aes(residuals(model))) + | |
| geom_histogram(fill = "#0099f9", color = "black") + | |
| theme_classic() + | |
| labs(title = "Residuals plot") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Make predictions on the test set | |
| predictions <- predict(model, test_set) | |
| # Convert to dataframe | |
| eval <- cbind(test_set$Weight, predictions) | |
| colnames(eval) <- c("Y", "Yhat") | |
| eval <- as.data.frame(eval) | |
| head(eval) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Evaluate model | |
| mse <- mean((eval$Y - eval$Yhat)^2) | |
| rmse <- sqrt(mse) |