【发布时间】:2014-09-19 18:28:37
【问题描述】:
这个问题是关于如何在具有边际效应的概率模型中编码变量选择(直接或通过调用一些预先存在的包)。
作为与 TLAPD 相关的blog post,我正在对电影的免费和商业可用性对这些电影的盗版水平的影响进行一点概率回归。
在 R 中运行概率的简单方法通常是通过glm,即:
probit <- glm(y ~ x1 + x2, data=data, family =binomial(link = "probit"))
但这对于解释来说是有问题的,因为它不提供边际效应。
通常,如果我想从概率回归中获得边际效应,我会定义这个函数(我不记得原始来源,但它是一个很受欢迎的函数,经常被重新发布):
mfxboot <- function(modform,dist,data,boot=500,digits=3){
x <- glm(modform, family=binomial(link=dist),data)
# get marginal effects
pdf <- ifelse(dist=="probit",
mean(dnorm(predict(x, type = "link"))),
mean(dlogis(predict(x, type = "link"))))
marginal.effects <- pdf*coef(x)
# start bootstrap
bootvals <- matrix(rep(NA,boot*length(coef(x))), nrow=boot)
set.seed(1111)
for(i in 1:boot){
samp1 <- data[sample(1:dim(data)[1],replace=T,dim(data)[1]),]
x1 <- glm(modform, family=binomial(link=dist),samp1)
pdf1 <- ifelse(dist=="probit",
mean(dnorm(predict(x, type = "link"))),
mean(dlogis(predict(x, type = "link"))))
bootvals[i,] <- pdf1*coef(x1)
}
res <- cbind(marginal.effects,apply(bootvals,2,sd),marginal.effects/apply(bootvals,2,sd))
if(names(x$coefficients[1])=="(Intercept)"){
res1 <- res[2:nrow(res),]
res2 <- matrix(as.numeric(sprintf(paste("%.",paste(digits,"f",sep=""),sep=""),res1)),nrow=dim(res1)[1])
rownames(res2) <- rownames(res1)
} else {
res2 <- matrix(as.numeric(sprintf(paste("%.",paste(digits,"f",sep=""),sep="")),nrow=dim(res)[1]))
rownames(res2) <- rownames(res)
}
colnames(res2) <- c("marginal.effect","standard.error","z.ratio")
return(res2)
}
然后像这样运行回归:
mfxboot(modform = "y ~ x1 + x2",
dist = "probit",
data = piracy)
但是使用这种方法我不知道我可以运行任何变量选择算法,例如向前、向后、逐步等。
解决此问题的最佳方法是什么?是否有更好的方法在 R 中运行概率,报告边际效应并允许自动模型选择?或者我应该专注于使用mfxboot 并使用该函数进行变量选择?
谢谢!
【问题讨论】:
-
这个问题的答案需要大量的统计成分。如果您将此迁移到 stats.SE 或在那里提出新问题,我将很乐意回答这个问题。
-
@fgnu 谢谢,虽然我尝试在 Crossvalidated/stats.SE 上提出一个非常相似/相关的问题,但他们发给我说它对 R 来说太具体了。感谢您在下面的回答。我会投票和评论。
标签: r regression feature-selection