Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
vcov.polr给出了负对角元素(包括mwe)_R - Fatal编程技术网

vcov.polr给出了负对角元素(包括mwe)

vcov.polr给出了负对角元素(包括mwe),r,R,我不太了解有序逻辑,所以可能我在做一些愚蠢的事情,但下面的内容似乎很简单,我不知道为什么vcov.polr会给我负对角线。我试图遵循vcov.polr中的代码,但是迷路了,所以我不知道出了什么问题。谢谢你的帮助 temp_fc <- structure(list(the_index = c(4, 3, 3, 4, 4, 1, 3, 2, 3, 3, 3, 4, 4, 4, 1, 4, 4, 1, 4, 3, 4, 3, 3, 1, 4, 2, 4, 4, 4, 4, 3, 4, 1, 4,

我不太了解有序逻辑,所以可能我在做一些愚蠢的事情,但下面的内容似乎很简单,我不知道为什么vcov.polr会给我负对角线。我试图遵循vcov.polr中的代码,但是迷路了,所以我不知道出了什么问题。谢谢你的帮助

temp_fc <-
structure(list(the_index = c(4, 3, 3, 4, 4, 1, 3, 2, 3, 3, 3,
4, 4, 4, 1, 4, 4, 1, 4, 3, 4, 3, 3, 1, 4, 2, 4, 4, 4, 4, 3, 4,
1, 4, 4, 3, 4, 4, 2, 4, 1, 4, 4, 2, 3, 1, 4, 2, 1, 1, 4, 1, 4,
4, 4, 4, 1, 4, 3, 1, 4, 3, 4, 1, 4, 2, 3, 1, 4, 4, 1, 3, 2, 3,
2, 3, 3, 4, 1, 1, 4, 4, 4, 4, 1, 4, 4, 2, 4, 4, 4, 1, 1, 4, 3,
2, 4, 4, 1, 1), age = c(39L, 40L, 23L, 33L, 24L, 22L, 15L, 43L,
59L, 58L, 20L, 39L, 33L, 21L, 34L, 44L, 27L, 53L, 60L, 34L, 17L,
20L, 19L, 21L, 57L, 56L, 36L, 30L, 16L, 17L, 16L, 71L, 49L, 42L,
72L, 36L, 18L, 19L, 62L, 20L, 55L, 79L, 69L, 46L, 28L, 50L, 40L,
21L, 57L, 31L, 38L, 60L, 56L, 59L, 21L, 26L, 53L, 49L, 51L, 59L,
32L, 56L, 25L, 46L, 42L, 70L, 65L, 50L, 42L, 53L, 29L, 63L, 34L,
29L, 65L, 31L, 70L, 52L, 76L, 37L, 24L, 28L, 45L, 47L, 48L, 55L,
65L, 28L, 28L, 31L, 24L, 41L, 43L, 34L, 69L, 65L, 48L, 24L, 32L,
18L)), .Names = c("the_index", "age"), row.names = c("1006",
"1040", "1085", "1115", "1130", "1144", "1162", "1164", "1176",
"1178", "1181", "1192", "1223", "1258", "1259", "1307", "1311",
"1330", "1338", "1368", "1401", "1411", "1443", "1444", "1515",
"1538", "1600", "1628", "1632", "1683", "1692", "1701", "1710",
"1784", "1790", "1793", "1799", "1873", "1897", "1904", "1908",
"1982", "1988", "2015", "2029", "2052", "2065", "2077", "2088",
"2095", "2163", "10345", "10355", "10372", "10388", "10397",
"10414", "10415", "10417", "10421", "10428", "10430", "10456",
"10480", "10490", "10492", "10509", "10587", "10600", "10607",
"10609", "10617", "10625", "10626", "10630", "10640", "10674",
"10684", "10686", "10687", "10700", "10703", "10713", "10730",
"10740", "10747", "10750", "10762", "10792", "10794", "10803",
"10820", "10824", "10830", "10833", "10835", "10857", "10888",
"10917", "10933"), class = "data.frame")

library(MASS)
model_temp <-factor(the_index) ~ age + I(age^2)
the_OL <- polr(model_temp,temp_fc,method="logistic", Hess=TRUE)
diag(vcov(the_OL)) #this gives negative values
#so of course the following will have an error:
summary(the_OL)

temp_fc我认为这是因为在模型公式中使用了非正交多项式;这似乎是造成计算问题的原因。通过您的代码,我得到:

> the_OL2 <- polr(model_temp,temp_fc,method="logistic", Hess=TRUE)
> AIC(the_OL2)
[1] 254.6934

两个模型报告的剩余偏差相同,两个模型的拟合值几乎相同(接近实际值)。

。我还是不明白发生了什么事。当我使用你的方法时,系数变化很大。你知道有什么在线资源可以让我读更多吗?非常感谢。当然,它们会改变@XuWang。变量
poly(age,2)1
poly(age,2)2
age
I(age^2)
形成的变量非常不同。后者是相关变量,我认为这就是导致拟合问题的原因。但模型是一样的。截距的变化是因为当协变量为零时,它们代表各组的响应值-现在不是
0
age
,在
age
的正交多项式基础上,它是0。我对这些东西不太熟悉,无法为您提供参考或资源,让您可以阅读更多内容。谢谢Gavin。不过我还是很困惑。它在Stata中工作,并给出相同的系数,Stata没有标准误差问题。我同意它们是高度相关的,但我认为这不应该是一个问题,因为相关性是.97,这是我以前从来没有遇到过的问题。@XuWang,所以请在包MASS中与函数的维护者讨论这个问题。不过,如果你不知道手头数据的真实结果,我会小心地说斯塔塔做对了。所有的
polr()
都在优化数值函数。您还可以查看rms包中的
lrm()
函数,该函数也可以拟合R中的比例赔率模型。查看它是否与非正交多项式有关。
> the_OL <- polr(factor(the_index) ~ poly(age, 2), temp_fc, method="logistic", 
+  Hess=TRUE)
> AIC(the_OL)
[1] 254.6934
> diag(vcov(the_OL))
poly(age, 2)1 poly(age, 2)2           1|2           2|3 
   3.38608675    3.65981581    0.05958267    0.04704916 
          3|4 
   0.04103139