2013-06-12 22 views
3

二次項在迴歸中很常見。下面是從約翰·福克斯(http://www.jstatsoft.org/v08/i15/paper找回迴歸模型中的二次項的最小效應

library(car) # For data 
library(splines) # For bs() 
library(effects) # For plotting 

data(Prestige) 

prestige.mod <- lm(prestige ~ log(income) + bs(education, df=3) + poly(women, 2), data=Prestige) 
summary(prestige.mod) 

test <- plot(all.effects(prestige.mod, default.levels=50)) 

enter image description here

一個例子是有R中的任何命令,以獲得最大/最小的二次效應的時候了,而無需手動導出在/策劃呢?

+0

我不確定我是否知道「二次效應的最小/最大值」是什麼意思。 – Roland

回答

1

如果我理解正確的話,我將逼近「婦女」的價值在其中的「影響最小」都可以看到:

idx <- which.min(predict(prestige.mod, newdata= data.frame(
     women=seq(min(Prestige$women), max(Prestige$women), length=100), 
     income=mean(Prestige$income, na.rm=TRUE), 
     education=mean(Prestige$education, na.rm=TRUE)))) 
idx 
#37 
#37 
# Just copy the argument to the newdata argument in predict call above 
# and get the value that produced the minimum 
seq(min(Prestige$women), max(Prestige$women), length=100)[idx] 
#[1] 35.45818 

包含在「newdata」價值序列使用該predict功能數據框架無疑是爲了繪製這些「效果」而在「引擎蓋下」發生的。