我正在研究连续变量(r_age)和二进制结果变量之间的关系。我已经意识到下面的LOESS图,这表明存在非线性关系。
我的问题是:在这样的非线性关系前面,什么是转换r_age变量以获得线性关系的最佳方法?
尚不清楚通过改变年龄使关系成为线性关系的意思。当然可以用x来创建多项式以给出y的值,但是不清楚为什么要这样做,除非它适合某些理论模型,
如果我们用黄土回归法大致重建您的地块:
df <- data.frame(x = c(20, 30, 40, 48, 50, 55, 65, 80),
y = c(0.27, 0.25, 0.23, 0.225, 0.21, 0.19, 0.24, 0.45))
# define loess model
loess_mod <- loess(y ~ x, data = df)
# Define points on x axis at which we want to predict
x_predict <- seq(20, 80, 0.1)
# Get predictions from loess model
loess_predict <- predict(loess_mod, newdata = x_predict)
# plot result of loess model
plot(x_predict, loess_predict, type = "l")
我们可以看到它可能很好地适合三次多项式,因此我们将尝试以下操作:
# define polynomial model
poly_mod <- lm(y ~ poly(x, 3, raw = TRUE), data = df)
# get polynomial model predictions
poly_predict <- predict(poly_mod, newdata = list(x = seq(20, 80, 0.1)))
lines(x_predict, poly_predict, col = "red", lty = 2)
这看起来很合理。如果我们检查模型,我们会看到该多项式曲线的系数:
summary(poly_mod)
#>
#> Call:
#> lm(formula = y ~ poly(x, 3, raw = TRUE), data = df)
#>
#> Residuals:
#> 1 2 3 4 5 6 7 8
#> 0.0038968 -0.0099525 -0.0002731 0.0161719 0.0043053 -0.0142652 -0.0010193 0.0011361
#>
#> Coefficients:
#> Estimate Std. Error t value Pr(>|t|)
#> (Intercept) 9.330e-02 8.227e-02 1.134 0.32013
#> poly(x, 3, raw = TRUE)1 1.767e-02 5.954e-03 2.968 0.04121 *
#> poly(x, 3, raw = TRUE)2 -5.471e-04 1.291e-04 -4.238 0.01328 *
#> poly(x, 3, raw = TRUE)3 4.772e-06 8.544e-07 5.585 0.00504 **
#> ---
#> Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#>
#> Residual standard error: 0.01225 on 4 degrees of freedom
#> Multiple R-squared: 0.987, Adjusted R-squared: 0.9773
#> F-statistic: 101.3 on 3 and 4 DF, p-value: 0.0003151
因此,从某种意义上讲,我们可以通过执行以下操作将x转换为与y的线性关系:
t_age <- 0.0933 + 0.0176 * df$x - 0.0005471 * df$x^2 + 0.000004772 * df$x^3
如果绘制它,我们可以看到关系是线性的:
plot(t_age, df$y)
abline(0, 1)
但是,除非您有充分的理由相信这样的关系确实存在,否则这似乎是没有意义的练习。