This function visualizes and models the relationship between length and weight (or any two continuous variables) using linear regression. It supports both standard and log-transformed models, producing a ggplot2-based plot with a fitted line, optional confidence interval shading, and annotations for the regression equation, R^2, and p-value. When save_output is TRUE, the plot and model summary are saved to the working directory as a PDF and text file, respectively.
LWR(
data,
log_transform = TRUE,
point_col = "black",
line_col = "red",
shade_col = "red",
point_size = 2,
line_size = 1,
alpha = 0.2,
main = "Length-Weight Relationship",
xlab = NULL,
ylab = NULL,
save_output = TRUE
)A list containing:
The fitted lm object
The estimated intercept (back-transformed if log_transform = TRUE)
The estimated slope
R-squared value
Correlation coefficient (r)
P-value for slope
The ggplot object for further customization
A data frame with at least two columns: the first for length, the second for weight.
Logical. Whether to apply a log-log transformation to the variables. Default is TRUE.
Color of the data points. Default is "black".
Color of the regression line. Default is "red".
Color for the confidence interval ribbon. Default is "red".
Size of the data points. Default is 2.
Size of the regression line. Default is 1.
Transparency for the confidence interval ribbon. Default is 0.2.
Title of the plot. Default is "Length-Weight Relationship".
Optional. Custom x-axis label. If NULL, a label is generated based on log_transform.
Optional. Custom y-axis label. If NULL, a label is generated based on log_transform.
Logical. Whether to save the plot as a PDF and the model summary as a text file. Default is TRUE.
data(LWdata, package = "aLBI")
result <- LWR(LWdata, log_transform = TRUE, save_output = FALSE)
print(result$plot)
Run the code above in your browser using DataLab