【问题标题】:Find intersection of 1:1 line and lm line查找 1:1 线和 lm 线的交点
【发布时间】:2017-11-07 00:08:12
【问题描述】:

我正在评估数值确定性模型的性能,并根据观察到的数据评估其预测性能。我制作了观察 (Vsurface) 与建模 (Vmod) 数据的散点图,拟合 lm(红线),并添加了 1:1 线。我想找到这两条线相交的点,这样我就可以记录模型从过度预测到预测不足的转变。是否有捷径可寻?这是lm的代码:

lm <- lm(Vmod~Vsurface, data = v)
summary(lm)
Call:
lm(formula = Vmod ~ Vsurface, data = v)

Residuals:
     Min       1Q   Median       3Q      Max 
-0.63267 -0.11995 -0.03618  0.13816  0.60314 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept)  0.20666    0.06087   3.395  0.00185 ** 
Vsurface     0.43721    0.06415   6.816 1.05e-07 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.2232 on 32 degrees of freedom
Multiple R-squared:  0.5921,    Adjusted R-squared:  0.5794 
F-statistic: 46.45 on 1 and 32 DF,  p-value: 1.047e-07

这是剧情代码:

ggplot(data = v, aes(x = Vsurface, y = Vmod)) +
  geom_point(col = "slateblue2") +
  geom_smooth(method = "lm", col = "red") +
  geom_abline(intercept = 0, slope = 1)

我正在使用 R markdown。

【问题讨论】:

  • 分析解决这个问题。 0.43721*x+0.20666 = x 所以x = 0.20666/(1-0.43721) = 0.3672062

标签: r markdown intercept


【解决方案1】:

只是为了明确声明G5W's 评论 - 模型是一个列表,可以像这样提取系数:

lmodel <- lm(Vmod~Vsurface, data = v)
x1 <- lmodel$coefficients[1]/(1-lmodel$coefficients[2])
### x1 is the intersection point

逐步编辑:

x <- rnorm(100,10,2)
y <- rnorm(100,15,3)

lmodel <- lm(y ~x)
lmodel$coefficients

拦截 = 13.6578378
x = 0.1283835

x1 <- lmodel$coefficients[1]/(1 - lmodel$coefficients[2])
x1

15.66955

【讨论】:

  • 当我将此脚本添加到我的 R markdown 文件并使用打印命令时,我得到这个: numeric(0) 我尝试将 x1 更改为 x.1 以防出现问题,但得到了结果相同。
  • 我编辑了答案,因为它有一个错字:“系数”而不是“系数”。对于任何对象 - 您可以在控制台行使用 str(objectName) 找到对象的结构。在这种情况下,$... 下的所有项目都可以像任何列表一样被访问/子集。
  • 我刚刚又试了一次,得到了同样的答案。当我在控制台中查看 x1 对象的结构时,我得到了“num(0)”。我仍然不确定自己做错了什么,但我肯定想弄清楚如何提取这样的交点,因为我将在工作中进行大量此类分析。
  • 由于您没有提供您的数据,我已经展示了一个示例数据示例。
  • 如果我的数据在多行的 csv 中(33 行唯一值,按列分组,以 Vmod 和 Vsurface 作为标题),我该怎么做?有时输出是每列的数万个唯一值,所以我想学习如何让脚本查看 csv 文件,而不是手动定义数据。
猜你喜欢
  • 2022-01-18
  • 2016-12-22
  • 1970-01-01
  • 1970-01-01
  • 2017-08-18
  • 1970-01-01
  • 2013-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多