当我在“stargazer”中为一个变量模型添加“ci.custom”的自定义置信区间时,R返回:“if中的错误(ncol(ci.custom [[i]])!= 2){ :参数长度为零“。当我做同样的事情,但有几个自变量,一切都很完美。
(1)ologit模型:
m <- polr(Risk_Taking_QNT ~ Wealth_log, data=F, Hess=T)
(2)置信区间:
cim <- exp(confint(m))
我明白了:
2.5%97.5%
1.006 1.223
(3)制作输出表:
stargazer(m, ci.custom = list(cim), ci = T, ci.level = 0.95, ci.separator = ";", apply.coef=exp, t.auto=FALSE, p.auto=FALSE, type="text")
R返回:“if中的错误(ncol(ci.custom [[i]])!= 2){:参数长度为零”
=============================================== =========================
与2变量模型相同的步骤:
(1)m <- polr(Risk_Taking_QNT ~ Wealth_log + Experience, data=F, Hess=T)
(2)cim <- exp(confint(m))
我明白了:
2.5%97.5%
Wealth_log 0.8768112 1.081713
经验1.2705479 1.530633
(3)stargazer(m, ci.custom = list(cim), ci = T, ci.level = 0.95, ci.separator = ";", apply.coef=exp, t.auto=FALSE, p.auto=FALSE, type="text")
我得到一个具有正确系数和间隔的普通表。我尝试过不同的变量,结果总是一样的:只适用于2个以上的变量。
谢谢大家的帮助!
=============================================== ================================================== ================================================== ===
以下是可重现的例子:
library(MASS)
library(stargazer)
数据
Y <- as.factor(c(3, 4, 4, 2, 1, 4, 3, 4, 3, 1))
X1 <- c(8.8, 6.2, 7.3, 7.3, 7.2, 6.4, 7.1, 5.5, 5.7, 7.2)
X2 <- c(7, 8, 9, 8, 8, 10, 9, 9, 7, 6)
带有1个变量的模型,我得到错误
m1 <- polr(Y ~ X1, Hess=T)
cim1 <- exp(confint(m1))
stargazer(m1, ci.custom = list(cim1), ci = T, ci.level = 0.95, ci.separator = ";", apply.coef=exp, t.auto=FALSE, p.auto=FALSE, type="text")
具有2个变量的模型可以正常工作
m2 <- polr(Y ~ X1 + X2, Hess=T)
cim2 <- exp(confint(m2))
stargazer(m2, ci.custom = list(cim2), ci = T, ci.level = 0.95, ci.separator = ";", apply.coef=exp, t.auto=FALSE, p.auto=FALSE, type="text")
答案 0 :(得分:0)
问题在于MASS:::confint.polr
,而不是stargazer
。错误消息在这里非常具有描述性。
library(MASS);
library(stargazer);
Y <- as.factor(c(3, 4, 4, 2, 1, 4, 3, 4, 3, 1))
X1 <- c(8.8, 6.2, 7.3, 7.3, 7.2, 6.4, 7.1, 5.5, 5.7, 7.2)
X2 <- c(7, 8, 9, 8, 8, 10, 9, 9, 7, 6)
m1 <- polr(Y ~ X1, Hess=T)
m2 <- polr(Y ~ X1 + X2, Hess=T)
dim( confint(m1) )
# NULL
dim( confint(m2) )
#[1] 2 2
对于具有一个协变量的模型,尺寸不会在confint.polr
中设置,但是stargazer需要2列(您可以看到这是有意义的,因为这相当于置信区间的上限和下限)。
使用方法lm
confint.lm
个对象中不存在此行为
m3 <- lm(mpg ~ 1, mtcars)
m4 <- lm(mpg ~ disp, mtcars)
dim( confint(m3) )
#[1] 1 2
dim( confint(m4) )
[1] 2 2
因此,为了解决这个问题,您可以在confint.polr
对象的polr
对象上运行m1 <- polr(Y ~ X1, Hess = TRUE)
cim1 <- exp(confint(m1))
dim(cim1) <- c(1, 2)
stargazer(m1, ci.custom = list(cim1), ci = T, ci.level = 0.95, ci.separator = ";", apply.coef=exp, t.auto=FALSE, p.auto=FALSE, type="text")
========================================
Dependent variable:
---------------------------
Y
----------------------------------------
X1 0.473
(0.104;1.537)
----------------------------------------
Observations 10
========================================
Note: *p<0.1; **p<0.05; ***p<0.01
时手动设置尺寸。
MASS
有点痛苦,但它确实有效。
另外,仅供参考,这种行为发生在confint
(MASS:::confint.polr
,MASS:::confint.glm
,MASS:::confint.nls
)的所有from django.forms import ModelForm
from django import forms
from django.contrib.auth.models import Group, User
class GroupAdminForm(ModelForm):
class Meta:
model = Group
group_users = forms.ModelMultipleChoiceField(label=u'Usuários deste Grupo', queryset=User.objects.all())
def __init__(self, *args, **kwargs):
super(GroupAdminForm, self).__init__(*args, **kwargs)
users_id = list()
# Avoid 'No exception supplied'
try:
users = self.instance.group.user_set.all()
for u in users:
users_id.append(u.id)
if self.fields.has_key('group_users'):
self.fields['group_users'].initial = users_id
except Exception, e:
pass
def clean(self):
group = self.cleaned_data['group']
group.save()
if group.user_set.all():
group.user_set.clear()
try:
users = self.cleaned_data['group_users']
for user in users:
group.user_set.add(user)
except:
return self.cleaned_data
return self.cleaned_data
方法上。