找到1:1行和lm行的交集

时间:2017-11-07 00:08:12

标签: r markdown intercept

我正在评估数值确定性模型的性能,并且我正在评估其对观察数据的预测性能。我制作了观察到的(Vsurface)vs建模(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工作。

1 个答案:

答案 0 :(得分:0)

只是明确说明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