如何解决如何使用 margins 包来评估因变量不同值的边际影响
我正在使用 margins
包 (vignette) 来计算相对于序数变量的边距。 margins 包试图“移植 Stata(封闭源代码)margins 的功能”。 This video(大约 4:25)表明,对于 Stata 中的序数概率模型,我可以评估一个变量在序数变量的不同值下的边际效应,在我的示例中为 x2
。
我尝试了 polr_1st_margins <- summary(margins(fit.polr,at = list(x2= 2:4)))
,这适用于示例数据。出于某种奇怪的原因,当我在实际数据上运行它时,出现错误。在这两种情况下,x2
都是一个因子变量(否则 polr
不会运行)。
我收到错误:Error in dat[,not_numeric,drop = FALSE] : incorrect number of dimensions
即使变量存在。
有人知道会发生什么吗?
示例代码:
library(sure) # for residual function and sample data sets
library(MASS) # for polr function
library(margins)
rm(df1)
df1 <- df1
df1$x2 <- df2$y
df1$x3 <- df2$x
df1$y <- df3$x/10
fit.polr <- polr(x2 ~ x + x3,data = df1,method = "probit")
summary(margins(fit.polr))
polr_1st_margins <- summary(margins(fit.polr,at = list(x2= 2:4)))
编辑
fit.polr2 <- polr(ordinal_dep_var ~ Dummy + Continuous + Dummy2 + as.factor(industry) + Urbanisation_Dummy + Size_Dummy,data = df2,method = "probit",Hess=TRUE)
summary(margins(fit.polr2))
polr_1st_margins <- summary(margins(fit.polr2,at = list(ordinal_dep_var= 0:3)))
Error in dat[,drop = FALSE] :
incorrect number of dimensions
不知何故,它没有找到变量,即使我可以看到它在那里。
数据
df2 <- structure(list(ordinal_dep_var = structure(c(4L,3L,4L,1L,2L,1L),.Label = c("0","1","2","3"),class = c("ordered","factor")),Dummy = c(0,1,0),Continuous = c(15.6862745098039,41.6666666666667,26.9230769230769,14.8514851485149,32.1428571428571,20,43.75,80,100,21.4285714285714,23.8095238095238,28.125,30.3030303030303,25,13.3333333333333,66.6666666666667,33.3333333333333,55.5555555555556,72.2222222222222,57.3033707865169,17.7777777777778,47.6190476190476,40,8.33333333333333,16.6666666666667,50,7.69230769230769,1.84089414858646,30,22.2222222222222,46.1538461538462,44.4444444444444,5.55555555555556,47.3684210526316,18.1818181818182,42.8571428571429,14.2857142857143,25.9259259259259,10,75,28.5714285714286,9.09090909090909,36.9230769230769,36.3636363636364,7.69230769230769),Dummy2 = structure(c(0,label = "Gift/informal payment requested: tax inspectorate?",format.stata = "%14.0f",class = c("haven_labelled","vctrs_vctr","double"),labels = c(Yes = 1,No = 2)),industry = structure(c(3,7,2,19,17,9,11,12,5,3,4,22,21,23,24,6,15,13,21),label = "Industry",format.stata = "%34.0g",labels = c(Textiles = 1,Leather = 2,Garments = 3,Agroindustry = 4,Food = 5,Beverages = 6,`Metals and machinery` = 7,Electronics = 8,`Chemicals and pharmaceutics` = 9,Construction = 10,`Wood and furniture` = 11,`Non-Metallic and plastic materials` = 12,Paper = 13,`Sport goods` = 14,`IT services` = 15,`Other manufacturing` = 16,Telecommunications = 17,`Accounting and finance` = 18,`Advertising and marketing` = 19,`Other services` = 20,`Retail and wholesale Trade` = 21,`Hotels and restaurants` = 22,Transport = 23,`Real estate and rental services` = 24,`Mining and quarrying` = 25,`Auto and auto components` = 26,`Other transport equipment` = 27,`Other unclassified` = 99)),Urbanisation_Dummy = structure(c(2L,.Label = c("City > 250","50k-250k","< 50k"),class = "factor"),Size_Dummy = structure(c(3L,.Label = c("Employees: < 10","Employees: 10-19","Employees: 20+"),class = "factor")),row.names = c(NA,-144L),class = c("data.table","data.frame"))
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。