2017-04-12 62 views
0

我有一些数据,我使用了earth模型。我对不同线条的斜坡感兴趣,但是看着模型总结我没有得到我的期望值。从地球模型中提取斜坡

library(earth) 
library(dplyr) 
library(ggplot2) 

d = structure(list(x = c(9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30), y = c(0.151534750704409, 
0.0348452707597105, -0.0913494247372798, -0.214465577974757, 
-0.365251164825619, -0.528214103496014, -0.614970081844732, 
-0.922572314358796, 
-1.15911158401926, -1.36432638285029, -1.51587576144429, -1.63708705686248, 
-1.7530889072188, -1.86142968143915, -1.98159646754281, -2.0994478459505, 
-2.23037530743309, -2.3421669680425, -2.40621060828366, -2.55432043723978, 
-2.73246980567199, -2.92496136528975)), .Names = c("x", "y"), row.names = 
c(NA, -22L), class = c("tbl_df", "tbl", "data.frame")) 

mod = earth(y ~ x, data = d) 

d$pred = predict(mod, newdata = d) 

summary(mod, style = 'pmax') 

这给了我这样的总结:

Call: earth(formula=y~x, data=d) 

y = 
    -1.314958 
    - 0.06811314 * pmax(0, x - 16) 
    + 0.1518165 * pmax(0, 19 - x) 
    - 0.05124021 * pmax(0, x - 19) 

Selected 4 of 4 terms, and 1 of 1 predictors 
Termination condition: RSq changed by less than 0.001 at 4 terms 
Importance: x 
Number of terms at each degree of interaction: 1 3 (additive model) 
GCV 0.004496406 RSS 0.04598597 GRSq 0.9953947 RSq 0.9976504 

然而,当我看到我的模型三个不同​​的坡度都期待负面:

ggplot(d, aes(x, y)) + 
    geom_point() + 
    geom_line(aes(x, pred)) + 
    theme(aspect.ratio = 1) 

slopes

我如何得到这3个负斜率的值?

+0

正系数乘以“-x”,产生负斜率。 – Ryan

回答

1

mod$coefficients给出系数。如果系数在-x te斜率将是系数的负数。您可以使用mod$coefficients %>% {ifelse(grepl('-x', rownames(.)) , -., .)}来获得斜坡(或者只是在精神上反转-x部分的标志)。