0

So I have various sets of data and I used ggplot to plot the average of the sets of data and got the below plots.

enter image description here

It shows response time during a trial over the entire length of the trial. I would like to estimate an equation to fit these plots. I was going to export the data to excel and try to do it in there, but so far it's been a nightmare and I haven't been able to get it to work anywhere close to it.

Using the top left plot as an example, I think I was able to use the code below to get the following plot with a fit linear regression line on it. I also clipped the end of the data since I don't need data after 400s.

enter image description here

Hoever, when I get the coefficients using the last line in my code below, I get the following values.

enter image description here

I was expecting the y intercept to be around 0.53-0.55ish so when it gives me 0.79 I am sure I am doing something wrong, but not sure what.

I tried using ggpubr but kept getting errors as well so now I am stuck and hoping someone can help. Much appreciated!

{r}
library(dplyr)
library(dbplyr)
library(ggplot2)
library(ggpubr)


CL111 <- clDataCleaned %>%
  select(partID, Layout, form, reliability, TS_LightOn, dur_cleaned) %>%
    filter(Layout == "1" & form == "1" & reliability == "1") 

#individual plot
ggplot(data = CL111) + geom_smooth(mapping = aes(x = TS_LightOn, y = dur_cleaned), method = "lm", se = FALSE, colour = "Green") +
labs(x = "TS Light On (Seconsd)", y = "TS Response Time (Seconds)", title = "Layout 1, Condition AO, INS High") +
  theme(plot.title = element_text(hjust = 0.5)) +
  stat_smooth(mapping = aes(x = TS_LightOn, y = dur_cleaned), method = "loess", se = TRUE) + xlim(0, 400) + ylim (0, 1.0) 

#find coefficients for best fit line

lm(CL111$dur_cleaned ~ CL111$TS_LightOn)
  • Is dur_cleaned the same as response time? – G5W Aug 13 '21 at 23:45
  • Yes, dur_cleaned is the responsetime – 23kingjollyrancher Aug 13 '21 at 23:45
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. – MrFlick Aug 13 '21 at 23:59
  • I don't think this question should have been closed. I think your regression line does not align with your intercept because this data does not meet the assumptions of linear regression. Either homogeneity or normality is probably violated. – Kat Aug 14 '21 at 00:40
  • 1
    Perhaps a different "close" reason, @Kat, but it is still nothing we can reproduce at the moment. 23kingjollyrancher, MrFlick's link (to make this a reproducible example) has a lot of good information. Bottom line: sample data that can reproduce what you see here is important; while it may be difficult to provide sufficient data in a question here using `dput(.)` (generally the best), there are other ways if you absolutely cannot reproduce this phenomenon with a *sample* of your real data. – r2evans Aug 14 '21 at 01:16
  • @Kat `geom_smooth()` uses the exact same method as `lm()` when you use `method="lm"`. The differences between results are not likely due to modeling assumptions. More than likely there's something inconsistent with the code and data and could be clarified with a reproducible example. But if the whole point is just to add the equation to the plot, then any of those existing answer should work and not exhibit the problem given here. – MrFlick Aug 14 '21 at 01:42
  • 1
    Also the method of clipping may be important: (eg) `xlim()` trims the data and then calculates the statistic. `coord_cartesian()` calculates the statistic and then "zooms" the plot. So use of `xlim` may explain the discrepancy. As @MrFlick says, without a MRE, we can only guess. Ah yes, `xlim` is there, hidden to the right. OP should try again, replacing `xlim` and `ylim` with `coord_cartesian()`. – Limey Aug 14 '21 at 06:23
  • All great reasons @r2evans, MrFlick, and Limey. Like I said, "I think." The other answers on SO are definitely things that 23kingjollyrancher could use. I only meant that that lack of alignment wasn't addressed in the linked question, but this question was closed as a duplicate. (It wouldn't let me "at" all 3 of you.) – Kat Aug 14 '21 at 18:42

0 Answers0