【问题标题】:How to calculate the Intersect of a ggplot line and a nonlinear function in r如何在r中计算ggplot线和非线性函数的交集
【发布时间】:2021-03-06 23:46:30
【问题描述】:

简介:我是 r 和堆栈溢出的新手......所以我正在写一篇学期论文,需要运行一些关于如何或更好地养成习惯的统计数据。 理想情况下,习惯形成是根据 Mitscherlich 定律,看起来像非线性回归和渐近线。一旦参与者达到他的平台期(定义为渐近线的 95% 间隔),人们就可以谈论一个既定的习惯......实际上这是有争议的......但我们正在复制 Lally 等人所做的一项研究。 2010 年(习惯是如何形成的:模拟现实世界中的习惯形成)所以我们必须以某种方式坚持某些标准

实际问题:第一步是获得线性和非线性回归的 R2。这是我管理的。
但由于某种原因,我无法获得非线性函数的交点(图片中的橙色点)和我的 95% 习惯高原线(图片中的紫色线)的 x 轴值... Here is an example of how an ideal graph looks like

但确切地说,这个 X 值对于组比较和稍后检查显着差异至关重要......

当然我已经用谷歌搜索了,但不知何故我无法理解针对其他或类似问题提出的解决方案......似乎无法在 ggplot 中使用 geom_point() & 因此必须使用 approx() 函数建立一个单独的公式,对吧?

也许有人可以帮助我...提前谢谢!

这是感兴趣的代码...

library(ggplot2)
library(patchwork)
library(stats)


days <- c(0:15)
score <- c(14,17,16,22,24,27,30,31,32,35,40,43,42,43,43)


df <- data.frame(days,score)




# red curve in graph
#This way the R2 for the nonlinear regression is obtained for later analisis

nonlinreg1 <- nls(score ~ SSasymp(days, Asym, R0, lrc), data = df)
summary(nonlinreg1)
RSS <- sum(residuals(nonlinreg1)^2)
TSS <- sum((df$score - mean(df$score))^2)
R.square.nonlinreg1 <- 1 - (RSS/TSS)
R.square.nonlinreg1

# purple line in graph
#Definition of plateau at 95% of asymptote reached

Asymp95 <- summary(nonlinreg1)$parameters[1,1] * 0.95

# define green line as the Asymptote

nls_line <- predict(nonlinreg1)

#plotting Asymptote (nls_line)

HabitReach95 <- approx(nls_line, df$days, xout = Asymp95)$y



# Now in GGplot 


ggplot(data=df,aes(x=days, y=score)) +
  geom_point()+

#HERE now from this intersect below, I would like to obtain the exact X-value

  geom_point(x = HabitReach95, y= Asymp95, aes(color="Intersect"), lwd=2) +






#this is the rest of ggplot code but I think it is not of interest for the described problem, but still just in case...

  geom_smooth(method=lm, aes(color="Linear Reg"), se=F) +
  geom_smooth(method="nls", formula=y~SSasymp(x, Asym, R0, lrc), aes(color="Non-Linear Reg"), se=F) +
  geom_hline(aes(color="Asymptote for non-linear Reg", yintercept=summary(nonlinreg1)$parameters[1,1])) +
  geom_hline(aes(color="Habit plateau at 95%", yintercept=Asymp95)) +
  xlab("Days of Experiment") + ylab("Automaticity Score Habit") +
  ggtitle("Test graph for participant") +
  theme(plot.title = element_text(hjust = 0.5))+
  #ylim(0,49)+ # Actual graph or scale for experiment
  scale_color_manual(values = c("green", "purple", "orange", "blue", "red"), name="Legend")


    
    
     

【问题讨论】:

  • 只看代码很难让任何人帮忙。如果您创建一个小的可重现示例以及预期的输出,则更容易提供帮助。阅读how to give a reproducible example
  • 非常好的观点!好吧,让我试着简化一下......
  • 希望它现在变得更清楚了...如果仍有问题,我很乐意进一步编辑

标签: r intersection non-linear-regression


【解决方案1】:

天哪,我太笨了……我已经用这条线计算过了!!!

HabitReach95 <- approx(nls_line, df$days, xout = Asymp95)$y

哈哈简直不敢相信……好吧,有时候你只见树木不见森林!

【讨论】:

    猜你喜欢
    • 2012-03-14
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2015-01-02
    相关资源
    最近更新 更多