2015-04-25 94 views
0

我意識到這個問題之前已經被問到過,但是看過所有的答案,他們是針對具體問題的,我無法找到我的獨特情況的答案。邏輯下標太長

我在R中輸入了下列內容,它對於第一個示例有效,但不是第二個,我不明白爲什麼。

爲GLM設置數據:

setwd("P:/STAT319") 
ucb2<-read.table('Berkeley.PoissonTwo.txt',header=TRUE) 
attach(ucb2) 

ucb2如下:使用因子變量,真假爲承認和NotAdmit

Count Admit Department Gender  
313 FALSE  A  Female  
512 TRUE  A  Female  
19 FALSE  A  Male  
89 TRUE  A  Male  
207 FALSE  B  Female  
353 TRUE  B  Female  
8 FALSE  B  Male  
17 TRUE  B  Male  
205 FALSE  C  Female  
120 TRUE  C  Female  
391 FALSE  C  Male  
202 TRUE  C  Male  
279 FALSE  D  Female  
138 TRUE  D  Female  
244 FALSE  D  Male  
131 TRUE  D  Male  
138 FALSE  E  Female  
53 TRUE  E  Female  
299 FALSE  E  Male  
94 TRUE  E  Male  
351 FALSE F  Female  
22 TRUE  F  Female  
317 FALSE  F  Male  
24 TRUE  F  Male 

Admit<-c(0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,1) 
fAdmit<-factor(Admit) 
rAdmit<-factor(Admit,labels=c("FALSE","TRUE")) 
glm2<-glm(Count~Admit+Department+Gender,family=poisson) 
glm2 

準備離開的方式交叉驗證

library(car) 
vif(glm2) 
# GVIF Df GVIF^(1/(2*Df)) 
# Admit   1 1    1 
# Department 1 5    1 
# Gender  1 1    1 
step(glm2) 
# Start: AIC=2272.73 
# Count ~ Admit + Department + Gender 
# 
# Df Deviance AIC 
# <none>   2097.7 2272.7 
# - Department 5 2257.2 2422.2 
# - Gender  1 2260.6 2433.6 
# - Admit  1 2327.7 2500.8 
# 
# Call: glm(formula = Count ~ Admit + Department + Gender, family = poisson) 
# 
# Coefficients: 
# (Intercept)  Admit DepartmentB DepartmentC 
# 5.82785  -0.45674  -0.46679  -0.01621 
# DepartmentD DepartmentE DepartmentF GenderMale 
# -0.16384  -0.46850  -0.26752  -0.38287 

# Degrees of Freedom: 23 Total (i.e. Null); 16 Residual 
# Null Deviance:  2650 
# Residual Deviance: 2098 AIC: 2273 

library(ipred) 
errorest(Count~Admit+Department+Gender,data=ucb2,model=glm,est.para=control.errorest(k=24)) 

# Call: 
# errorest.data.frame(formula = Count ~ Admit + Department + Gender, 
#      data = ucb2, model = glm, est.para = control.errorest(k = # 24)) 
# 
# 24-fold cross-validation estimator of root mean squared error 
# 
# Root mean squared error: 180.5741 

所以第一個人使用數據如圖所示。我們用相同的研究做, 我們不得不重新排列數據,並進行Logistic迴歸:

ucb1<-read.table('Monday.Late.txt',header=TRUE) 
attach(ucb1) 
# The following object is masked _by_ .GlobalEnv: 
# 
# Admit 

# The following objects are masked from ucb2: 
# 
# Admit, Department, Gender 

y<-cbind(ucb1[,1],ucb1[,2]) 
glm1<-glm(y~Gender+Department,family=binomial) 

的數據,這是如下:

Admit NotAdmit Gender Department  
512 313 female a  
353 207 female b  
120 205 female c  
138 279 female d  
53 138 female e  
22 351 female f  
89 19 male a  
17 8 male b  
202 391 male c  
131 244 male d  
94 299 male e  
24 317 male f  

設置爲這個新的數據了留出一個:

vif(glm1) 
# GVIF Df GVIF^(1/(2*Df)) 
# Gender  1.384903 1  1.176819 
# Department 1.384903 5  1.033099 
step(glm1) 
# Start: AIC=103.14 
# y ~ Gender + Department 

# Df Deviance AIC 
# - Gender  1 21.74 102.68 
# <none>    20.20 103.14 
# - Department 5 783.61 856.55 
# 
# Step: AIC=102.68 
# y ~ Department 
# 
# Df Deviance AIC 
# <none>    21.74 102.68 
# - Department 5 877.06 948.00 
# 
# Call: glm(formula = y ~ Department, family = binomial) 
# 
# Coefficients: 
# (Intercept) Departmentb Departmentc Departmentd 
# 0.59346  -0.05059  -1.20915  -1.25833 
# Departmente Departmentf 
# -1.68296  -3.26911 
# 
# Degrees of Freedom: 11 Total (i.e. Null); 6 Residual 
# Null Deviance:  877.1 
# Residual Deviance: 21.74 AIC: 102.7 

到目前爲止,一切都很好,但現在的問題出現了:

errorest(y~Gender+Department,data=ucb1,model=glm,est.para=control.errorest(k=12)) 
Error in xj[i, , drop = FALSE] : (subscript) logical subscript too long 

爲什麼會發生這種情況?我試過其他值K,不知道是#是什麼意思k值取 - 我認爲它的意思是行

然後我嘗試同樣的數據的數量,安排了不同的方式:

ucb1a<-read.table('Berkeley.Rearranged.txt',header=TRUE) 
attach(ucb1a) 
ucb1a 

這是數據從之前

Admitted Not_Admit Depart Genders 
1  512  313  A Female 
2  89  19  A Male 
3  353  207  B Female 
4  17   8  B Male 
5  120  205  C Female 
6  202  391  C Male 
7  138  279  D Female 
8  131  244  D Male 
9  53  138  E Female 
10  94  299  E Male 
11  22  351  F Female 
12  24  317  F Male 

重排,然後

y<-cbind(ucb1[,1],ucb1[,2]) 
glm1a<-glm(y~Genders+Depart,family=binomial) 
vif(glm1a) 
# GVIF Df GVIF^(1/(2*Df)) 
# Gender  1.384903 1  1.176819 
# Department 1.384903 5  1.033099 

step(glm1a) 
# Start: AIC=103.14 
# y ~ Gender + Department 
# 
# Df Deviance AIC 
# - Gender  1 21.74 102.68 
# <none>    20.20 103.14 
# - Department 5 783.61 856.55 
# 
# Step: AIC=102.68 
# y ~ Department 
# 
# Df Deviance AIC 
# <none>    21.74 102.68 
# - Department 5 877.06 948.00 
# 
# Call: glm(formula = y ~ Department, family = binomial) 
# 
# Coefficients: 
# (Intercept) Departmentb Departmentc Departmentd 
# 0.59346  -0.05059  -1.20915  -1.25833 
# Departmente Departmentf 
# -1.68296  -3.26911 
# 
# Degrees of Freedom: 11 Total (i.e. Null); 6 Residual 
# Null Deviance:  877.1 
# Residual Deviance: 21.74 AIC: 102.7 

再次,到目前爲止好,但再一次,發生這種情況:

errorest(y~Gender+Department,data=ucb1a,model=glm,est.para=control.errorest(k=12)) 
Error in xj[i, , drop = FALSE] : (subscript) logical subscript too long 

相信我,我又試過其他號碼K,我不明白爲什麼這個人會是錯誤的。因此,如果任何人有任何想法,對於(下標)邏輯下標的這個特定示例太長,請回復此問題。

+0

「附加」你的數據集的原因是什麼? – nicola

+0

這是我們被告知在我們的Stats類中完成的方式。我聽說過使用Command'with' - 你會推薦做什麼?謝謝。克里斯禮來。 –

+0

@ChristopherBrentLilly你有解決這個問題嗎?我面臨同樣的問題! – woodhead92

回答

0

當您的對象大小不同時會出現此問題。 我認爲你的問題來自attach(),但我不確定。嘗試沒有它的代碼,或者你可以嘗試使用()。 你應該檢查爲什麼你必須先使用attach(),然後才能像nicola指出的那樣使用它。另外,我不確定你試圖用它達到什麼目的。

您可以在以下功能的幫助下部分中看到: 良好做法

附加有改變的搜索路徑的副作用,這很容易導致一個特定的名字錯了對象被人發現。人們經常忘記分離數據庫。

在交互式使用中,通常優先使用attach/detach,除非save()生成的文件在這種情況下attach()是load()的(安全)包裝器。