library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
This is an R Markdown presentation. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document.
“Here’s an example showing how linear regression can be implemented in biology:”
#This data is my numbers just rand numbers
my_data <- data.frame(
body_size = c(2.2, 3.3, 4.1, 5.5, 7.6),
metabolism = c(10, 20, 30, 40, 50)
)
# Fit a linear model
model <- lm(metabolism ~ body_size,data = my_data)
print(summary(model))
##
## Call:
## lm(formula = metabolism ~ body_size, data = my_data)
##
## Residuals:
## 1 2 3 4 5
## -2.6092 -0.7844 3.2701 2.8653 -2.7418
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -3.7411 3.9433 -0.949 0.41275
## body_size 7.4320 0.8031 9.254 0.00267 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.359 on 3 degrees of freedom
## Multiple R-squared: 0.9662, Adjusted R-squared: 0.9549
## F-statistic: 85.64 on 1 and 3 DF, p-value: 0.00267
library(ggplot2)
ggplot(my_data, aes(x = body_size, y =metabolism )) +
geom_point(color = "blue") +
labs(title = "Body Mass vs. Metabolic Rate",
x = "Body Mass (grams)",
y = "Metabolic Rate (kcal/day)") +
theme_minimal()
#Slide 5: Fitting the Linear Regression Model
model <- lm(metabolism ~ body_size, data = my_data)
print(summary(model))
##
## Call:
## lm(formula = metabolism ~ body_size, data = my_data)
##
## Residuals:
## 1 2 3 4 5
## -2.6092 -0.7844 3.2701 2.8653 -2.7418
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -3.7411 3.9433 -0.949 0.41275
## body_size 7.4320 0.8031 9.254 0.00267 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.359 on 3 degrees of freedom
## Multiple R-squared: 0.9662, Adjusted R-squared: 0.9549
## F-statistic: 85.64 on 1 and 3 DF, p-value: 0.00267
##Slide 6: 3-D Scatterplot using Plotly
library(plotly)
# Adding a third variable for demonstration
wing_length <- rnorm(5, mean = 30, sd = 5)
my_data$wing_length <- wing_length
plot_ly(my_data, x = ~body_size, y = ~metabolism, z = ~wing_length, color = ~body_size, colors = c('#BF382A', '#0C4B8E')) %>%
add_markers() %>%
layout(scene = list(xaxis = list(title = 'Body Mass(grams)'),
yaxis = list(title = 'Metabolic Rate'),
zaxis = list(title = 'Wing Length')))
my_data$residuals <- residuals(model)
ggplot(my_data, aes(x = body_size, y = residuals)) +
geom_point(color = "red") +
geom_hline(yintercept = 0, linetype = "dashed") +
labs(title = "Residual Plot",
x = "Body Mass (grams)",
y = "Residuals") +
theme_minimal()
##Slide 8: R code for Linear Regression
# Fit the linear regression model
model <- lm(metabolism ~ body_size, data = my_data)
print(model)
##
## Call:
## lm(formula = metabolism ~ body_size, data = my_data)
##
## Coefficients:
## (Intercept) body_size
## -3.741 7.432