首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何计算r中in图线与非线性函数的相交

如何计算r中in图线与非线性函数的相交
EN

Stack Overflow用户
提问于 2021-03-06 23:46:30
回答 1查看 179关注 0票数 0

介绍:我对r和堆栈overflow...So都是新手,我正在做一篇学期论文,需要对如何或更好地养成习惯进行一些统计。理想情况下,习惯形成是根据米舍利希定律&看起来像是一个非线性回归和渐近线。一旦参与者到达他的高原(定义为95%的时间间隔到渐近线),人们就可以说出一个既定的习惯.其实这是有争议的..。但我们正在复制拉利等人的一项研究。2010年(如何形成习惯:模拟现实世界中的习惯形成),所以我们必须坚持认证标准。

实际问题:第一步是得到线性和非线性回归的R2 .这是我做的。

但由于某些原因,我无法获得非线性函数的相交(图中的橙色点)和95%的习惯平台线(图中的紫色线)的x轴值。下面是一个关于理想图的示例

但是这个X值对于组比较和以后的显着性差异检验是至关重要的.

当然,我已经在谷歌上搜索过了,但不知何故,我无法理解所给出的其他或类似问题的解决方案。似乎不能用geom_point()在ggplot中这样做&因此必须使用大约()函数构建一个独立的公式,对吗?

也许有人能帮我..。Tks提前!

这是兴趣代码..。

代码语言:javascript
复制
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")
EN

回答 1

Stack Overflow用户

发布于 2021-03-07 16:54:04

OMg我太蠢了..。我已经用这一行算过了!

代码语言:javascript
复制
HabitReach95 <- approx(nls_line, df$days, xout = Asymp95)$y

哈哈真不敢相信..。嗯,有时候你看不见森林里的树木!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66511833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档