2017-04-12 86 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部分的標誌)。