lm()之后如何在R中复制Stata的"边距"

Nat*_*107 2 r sequence prediction marginal-effects

来自Stata:

margins, at(age=40)
Run Code Online (Sandbox Code Playgroud)

要了解产生预期结果的原因,让我们告诉您,如果要输入.利润率将报告整体利润率 - 保持不变的利润率.因为我们的模型是逻辑的,所以将报告预测概率的平均值.at()选项将一个或多个协变量固定为指定的值,并且可以与因子和连续变量一起使用.因此,如果你输入

margins, at(age=40)
Run Code Online (Sandbox Code Playgroud)

然后利润率将数据平均每个人的回答,设定年龄= 40.

有人可以帮助我哪个包有用吗?我已经尝试找到子集数据的预测值的平均值,但它不适用于序列,例如边距,(年龄= 40(1)50).

Hac*_*k-R 7

有许多方法可以在R中获得边际效应.

你应该明白,Stata margins, at 只是在手段或代表点评估的边际效应(见本文和文档).

我认为你最喜欢这个解决方案,因为它与你习惯的最相似:

library(devtools)
install_github("leeper/margins")
Run Code Online (Sandbox Code Playgroud)

资料来源:https://github.com/leeper/margins

margin是努力将Stata(闭源)边距命令移植到R作为S3泛型方法来计算模型对象中包含的协变量的边际效应(或"部分效应")(如类"lm"和"glm"的那些) ).新"边距"类的绘图方法还可以使用marginsplot命令.

library(margins)
x <- lm(mpg ~ cyl * hp + wt, data = mtcars)
(m <- margins(x))
Run Code Online (Sandbox Code Playgroud)
     cyl       hp       wt 
 0.03814 -0.04632 -3.11981
Run Code Online (Sandbox Code Playgroud)

另请参见此包中的predictioncommand(?prediction).

除此之外,这里还有我编译的其他一些解决方案:

一.erer(包)

maBina() command
Run Code Online (Sandbox Code Playgroud)

http://cran.r-project.org/web/packages/erer/erer.pdf

II.mfxboot

mfxboot <- function(modform,dist,data,boot=1000,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)}
Run Code Online (Sandbox Code Playgroud)

资料来源:http://www.r-bloggers.com/probitlogit-marginal-effects-in-r/

III.资料来源:R probit回归边际效应

x1 = rbinom(100,1,.5)
x2 = rbinom(100,1,.3)
x3 = rbinom(100,1,.9)
ystar = -.5  + x1 + x2 - x3 + rnorm(100)
y = ifelse(ystar>0,1,0)
probit = glm(y~x1 + x2 + x3, family=binomial(link='probit'))
xbar <- as.matrix(mean(cbind(1,ttt[1:3])))
Run Code Online (Sandbox Code Playgroud)

现在是图形,即x1,x2和x3的边际效应

library(arm)
curve(invlogit(1.6*(probit$coef[1] + probit$coef[2]*x + probit$coef[3]*xbar[3] + probit$coef[4]*xbar[4]))) #x1
curve(invlogit(1.6*(probit$coef[1] + probit$coef[2]*xbar[2] + probit$coef[3]*x + probit$coef[4]*xbar[4]))) #x2
curve(invlogit(1.6*(probit$coef[1] + probit$coef[2]*xbar[2] + probit$coef[3]*xbar[3] + probit$coef[4]*x))) #x3

library(AER)
data(SwissLabor)
mfx1 <- mfxboot(participation ~ . + I(age^2),"probit",SwissLabor)
mfx2 <- mfxboot(participation ~ . + I(age^2),"logit",SwissLabor)
mfx3 <- mfxboot(participation ~ . + I(age^2),"probit",SwissLabor,boot=100,digits=4)

mfxdat <- data.frame(cbind(rownames(mfx1),mfx1))
mfxdat$me <- as.numeric(as.character(mfxdat$marginal.effect))
mfxdat$se <- as.numeric(as.character(mfxdat$standard.error))


# coefplot
library(ggplot2)
ggplot(mfxdat, aes(V1, marginal.effect,ymin = me - 2*se,ymax= me + 2*se)) +
  scale_x_discrete('Variable') +
  scale_y_continuous('Marginal Effect',limits=c(-0.5,1)) +
  theme_bw() +
  geom_errorbar(aes(x = V1, y = me),size=.3,width=.2) +
  geom_point(aes(x = V1, y = me)) +
  geom_hline(yintercept=0) +
  coord_flip() +
  opts(title="Marginal Effects with 95% Confidence Intervals")
Run Code Online (Sandbox Code Playgroud)