# See for visualization # https://r-graph-gallery.com/ # Correlation and Regression # Village Weaver Data # Set working directory and Import data weaver <- read.csv("villageweaver.csv") # ------------------------------------------------------------- # CORRELATION # ------------------------------------------------------------- # Check whether wing length and weight are correlated. ## Step 1: Check the variables summary(weaver$wing) summary(weaver$weight) ## Step 2: Plot the relationship (Scatterplot) library(ggplot2) ggplot(weaver, aes(x = wing, y = weight)) + geom_point() + labs(x = "Wing Length (mm)", y = "Weight (g)") + theme_classic() ## Step 3: Calculate correlation shapiro.test(weaver$weight) cor(weaver$wing, weaver$weight, method = "spearman") cor.test(weaver$wing, weaver$weight, method = "spearman") ### Interpretation # Correlation coefficient (r) # r > 0 : positive relationship # r < 0 : negative relationship # r = 0 : no relationship # There is a positive correlation between wing length and weight (r = 0.63, p < 0.001). # Birds with longer wings tend to be heavier. # ------------------------------------------------------------- # 3. LINEAR REGRESSION # ------------------------------------------------------------- ### Goal: Predict weight from wing length. ## Step 1: Fit a linear regression model model1 <- lm(weight ~ wing, data = weaver) summary(model1) ### Look at: # Estimate for wing → slope # p-value → significance of effect # R-squared → how much variation is explained # Wing length significantly predicts weight (p < 0.001). # The model explains about 54% of the variation in weight. # For each 1 mm increase in wing length, weight increases by about 0.88 g. ## Step 2: Plot regression line with ggplot ggplot(weaver, aes(x = wing, y = weight)) + geom_point() + geom_smooth(method = "lm") + labs(x = "Wing Length (mm)", y = "Weight (g)") + theme_classic() # ------------------------------------------------------------- # 4. MULTIPLE REGRESSION # ------------------------------------------------------------- ### Predict weight using wing and sex. model2 <- lm(weight ~ wing + sex, data = weaver) summary(model2) ggplot(weaver, aes(x = wing, y = weight, colour=sex)) + geom_point() + geom_smooth(method = "lm") + labs(x = "Wing Length (mm)", y = "Weight (g)") + theme_classic()