gpt4 book ai didi

r - 如何在 lm() 之后在 R 中复制 Stata 的 "margins at"

转载 作者:行者123 更新时间:2023-12-05 09:19:54 25 4
gpt4 key购买 nike

来自 Stata:

margins, at(age=40)

要了解为什么会产生所需的结果,让我们告诉您,如果您要输入.边距margins 将报告整体利润率——不保持不变的利润率。因为我们的模型是逻辑的,将报告预测概率的平均值。 at() 选项修复指定值的一个或多个协变量,可与因子和连续变量一起使用变量。因此,如果您输入

margins, at(age=40)

然后边距将在数据上取平均值每个人的响应,设置 age=40。

有人能帮我看看哪个包有用吗?我已经尝试找到子集数据的预测值的均值,但它不适用于序列,例如边距,at(age=40 (1)50)。

最佳答案

有很多方法可以在 R 中获得边际效应。

您应该了解 Stata 的利润率,在 只是在均值或代表点评估的边际效应(参见this 和文档)。

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

library(devtools)
install_github("leeper/margins")

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

margins is an effort to port Stata's (closed source) margins command to R as an S3 generic method for calculating the marginal effects (or "partial effects") of covariates included in model objects (like those of classes "lm" and "glm"). A plot method for the new "margins" class additionally ports the marginsplot command.

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

另请参阅此包中的 prediction 命令 (?prediction)。

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

我。 erer(包)

maBina() command

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

二。启动

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)}

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

三。来源:R probit regression marginal effects

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])))

现在上图,即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")

关于r - 如何在 lm() 之后在 R 中复制 Stata 的 "margins at",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39130623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com