我有两个问题与R中的事情几乎完全相关。
我在R中运行序数回归分析。因变量有三个级别(0 =无动作; 1 =警告; 2 =制裁)。
我在lrm
包中使用rms
命令:
print( res1<- lrm(Y ~ x1+x2+x3+x4+x5+x6, y=TRUE, x=TRUE, data=mydata))
我根本无法理解我?predict.lrm
生成的信息。我想要做的是计算因变量的每个级别的所有解释变量的边际效应。在Stata中,这非常简单:mfx compute,predict(结果(#0)); mfx计算,预测(结果(#2))和mfx计算,预测(结果(#3))。
所以我的第一个问题是:如何为R中的每个结果生成边际效应?请记住,我的R技能并不高级。
第二个问题与交互效应有关,我需要将其包含在同一模型中:
print( res1<- lrm(Y ~ x1+x2+x3+x4+x5+x6+x5*x6, y=TRUE, x=TRUE, data=mydata))
如果我知道第一个问题的答案,我会在包含交互项时产生边际效应。然后,我会绘制交互项的预测值。
所以第二个问题是:如何在交互项中绘制变量的影响(预测值)?
非常感谢!
编辑: 来自我的数据集的小样本(只有一个国家/地区)
dput(mydatasample)
structure(list(year = 1989:2014, country = structure(c(1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "Canada", class = "factor"),
id = structure(1:26, .Label = c("CAN 1989", "CAN 1990", "CAN 1991",
"CAN 1992", "CAN 1993", "CAN 1994", "CAN 1995", "CAN 1996",
"CAN 1997", "CAN 1998", "CAN 1999", "CAN 2000", "CAN 2001",
"CAN 2002", "CAN 2003", "CAN 2004", "CAN 2005", "CAN 2006",
"CAN 2007", "CAN 2008", "CAN 2009", "CAN 2010", "CAN 2011",
"CAN 2012", "CAN 2013", "CAN 2014"), class = "factor"), stage1 = c(1L,
1L, 0L, 0L, 0L, 0L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 0L, 0L, 0L,
0L, 2L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L), x1 = c(1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 0L, 0L, 0L), x2 = c(1L, 2L, 1L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 2L, 1L, 1L, 1L,
2L, 1L, 2L, 2L, 2L, 2L), x3 = c(9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 8L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L), x4 = c(31L, 31L, 31L, 31L,
31L, 30L, 30L, 30L, 31L, 30L, 29L, 30L, 28L, 28L, 28L, 27L,
29L, 29L, 29L, 28L, 25L, 24L, 23L, NA, NA, NA), x5 = structure(1:26, .Label = c("17,12528685",
"17,14022279", "17,15382785", "17,16610202", "17,17704534",
"17,18665779", "17,19493938", "17,20571103", "17,21628118",
"17,22493732", "17,23321101", "17,242041", "17,25213621",
"17,26110753", "17,27106985", "17,2810902", "17,29094924",
"17,29891768", "17,30861622", "17,31943819", "17,33088659",
"17,34202619", "17,35190237", "17,36381421", "17,37537139",
"17,38618117"), class = "factor"), x5.1 = c(0L, 0L, 0L, 0L,
1L, 0L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 1L,
0L, 0L, 0L, 0L, 1L, 1L, 0L)), .Names = c("year", "country",
"id", "stage1", "x1", "x2", "x3", "x4", "x5", "x5.1"), class = "data.frame", row.names = c(NA,
-26L))