I would like to know if there is a statistical analysis to determine differences between regions of a curve/regression across two treatments. I know there are several statistical analyses for determining if there are differences between two regressions/curves by a variable overall, but I don't know of any that can identify differences by region.
Let's say that X = Light_uE, Y = CellspermL, and Species is the treatment condition (which should be used to generate two different plots). How do I tell if there is a significant difference between, say, the curve at the Light_uE (X) conditions from 10-20 or 20-30 between Species? Again, not if the curves are different overall. This question seems similar to what I am seeking, only it examines the entire dataset rather than just a small segment, and uses a polynomial function rather than a piecewise regression. (I also don't think an ANOVA would be appropriate for this, and I would like to stick with ggplot when possible.)
I have provided a reproducible dataframe, as well as code to plot this data if you want to visualize it. Thanks for any help.
example_curves <- data.frame( Light_uE = c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100), CellspermL = c(1000000, 2000000, 3000000, 4000000, 5000000, 4000000, 3000000, 2000000, 1000000, 500000, 2000000, 2500000, 3500000, 3600000, 4000000, 3900000, 3000000, 2000000, 1000000, 500000), Species = c("A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B")) print(example_curves) # Piecewise plotting library(segmented) library(gridExtra) library(ggplot2) create_piecewise_plot <- function(data, title, initial_psi) { # Fit a linear model lm_model <- lm(CellspermL ~ Light_uE, data = data) # Apply segmented function with manually specified initial breakpoint seg_model <- segmented( lm_model, seg.Z = ~Light_uE, psi = initial_psi, # Manually specify the starting point control = seg.control(display = FALSE) # Disable extra output ) data$fitted_values <- fitted(seg_model) p <- ggplot(data, aes(x = Light_uE, y = CellspermL)) + geom_point() + geom_line(aes(y = fitted_values), color = 'blue') + labs( title = title, x = "Light (uE)", y = "Cells per mL" ) + theme_minimal() return(p) } # Creation of two different plots by Species data_species_a <- subset(example_curves, Species == "A") data_species_b <- subset(example_curves, Species == "B") range_a <- range(data_species_a$Light_uE) range_b <- range(data_species_b$Light_uE) # Ensure initial_psi values are within the range plot_species_a <- create_piecewise_plot(data_species_a, "Species A", initial_psi = 50) # Choose a valid value within range_a plot_species_b <- create_piecewise_plot(data_species_b, "Species B", initial_psi = 50) # Choose a valid value within range_b # Arrange the plots horizontally using grid.arrange grid.arrange(plot_species_a, plot_species_b, ncol = 2) 