2016-06-19 5 views
0

먼저 마코프 천이 행렬을 계산하고 그 지수를 취합니다. 첫 번째 목표를 달성하기 위해 나는 markovchainFit 함수를 markovchain 패키지 안에 넣고 매트릭스가 아닌 data.frame을 반환합니다. 그래서 지수를 얻기 전에 행렬로 변환해야합니다. 내 R의 코드가markovchainFit 객체에서 천이 행렬에 액세스하십시오.

################################# 
# Estimate Transition Matrix # 
################################# 
setwd("G:/Data_backup/GDP_per_Capita") 

library("foreign") 
library("Hmisc") 
mydata <- stata.get("G:/Data_backup/GDP_per_Capita/states.dta") 
mydata 

library(markovchain) 
library(expm) 

rgdp_e=mydata[,2:7] 
rgdp_o=mydata[,8:13] 
createSequenceMatrix(rgdp_e) 
rgdp_e_trans<-markovchainFit(data=rgdp_e,,method="bootstrap",nboot=5, name="Bootstrap Mc") 
rgdp_e_trans<-as.numeric(unlist(rgdp_e_trans)) 
rgdp_e_trans<-as.matrix(rgdp_e_trans) 
is.matrix(rgdp_e_trans) 
rgdp_e_trans %^% 1/5 

rgdp_e_trans처럼 데이터 프레임이고, 나는 숫자 행렬로 변환하려고합니다. 그것은 is.matrix 명령을 사용하여 테스트 할 때 작동하는 것처럼 보입니다. 그러나 마지막 줄이 나에게 오류를 줄 유래의 일부 검색 작업 후

Error in rgdp_e_trans %^% 2 : 
    (list) object cannot be coerced to type 'double' 

를했다, 나는 비슷한 문제를 공유 this question을 찾아 ''더블로 객체를 강요 rgdp_e_trans<-as.numeric(unlist(rgdp_e_trans))를 사용하지만, 그것은 작동하지 않을 것 같다. 는 게다가, data.frame의 rgdp_e_trans은 콘솔의 출력은 문제가 있거나 지수를 계산하는 다른 방법을 해결하기 위해 어떤 제안이

> rgdp_e=mydata[,2:7] 
> rgdp_o=mydata[,8:13] 
> createSequenceMatrix(rgdp_e) 
Error: not compatible with STRSXP 
> rgdp_e_trans<-markovchainFit(data=rgdp_e,,method="bootstrap",nboot=5, name="Bootstrap Mc") 
> rgdp_e_trans 
$estimate 
      1   2   3   4   5 
1 0.6172840 0.18930041 0.09053498 0.074074074 0.02880658 
2 0.1125828 0.59602649 0.28476821 0.006622517 0.00000000 
3 0.0000000 0.03846154 0.60256410 0.358974359 0.00000000 
4 0.0000000 0.01162791 0.03488372 0.691860465 0.26162791 
5 0.0000000 0.00000000 0.00000000 0.044247788 0.95575221 


> rgdp_e_trans<-as.numeric(unlist(rgdp_e_trans)) 
Error: (list) object cannot be coerced to type 'double' 
> rgdp_e_trans<-as.matrix(rgdp_e_trans) 
> is.matrix(rgdp_e_trans) 
[1] TRUE 
> rgdp_e_trans %^% 1/5 
Error in rgdp_e_trans %^% 1 : 
    (list) object cannot be coerced to type 'double' 
> 

처럼 어떤 인자 또는 문자를 포함하지? 고맙습니다.


추가 :

> str(rgdp_e_trans) 
List of 1 
$ estimate:Formal class 'markovchain' [package "markovchain"] with 4 slots 
    .. [email protected] states   : chr [1:5] "1" "2" "3" "4" ... 
    .. [email protected] byrow   : logi TRUE 
    .. [email protected] transitionMatrix: num [1:5, 1:5] 0.617 0.113 0 0 0 ... 
    .. .. ..- attr(*, "dimnames")=List of 2 
    .. .. .. ..$ : chr [1:5] "1" "2" "3" "4" ... 
    .. .. .. ..$ : chr [1:5] "1" "2" "3" "4" ... 
    .. [email protected] name   : chr "Bootstrap Mc" 

나는 당신으로 markovchainFit에 의해 반환되는 객체에서 직접 전이 행렬에 액세스 할 수 있습니다

rgdp_e=mydata[,2:7] 
rgdp_o=mydata[,8:13] 
createSequenceMatrix(rgdp_e) 
rgdp_e_trans<-markovchainFit(data=rgdp_e,,method="bootstrap",nboot=5, name="Bootstrap Mc") 
rgdp_e_trans 
str(rgdp_e_trans) 
# rgdp_e_trans<-as.numeric(unlist(rgdp_e_trans)) 
# rgdp_e_trans<-as.matrix(rgdp_e_trans) 
# is.matrix(rgdp_e_trans) 
rgdp_e_trans$estimate %^% 1/5 
+0

'rgdp_e_trans $ estimate %^% 1/5'을 사용해보세요. –

+0

작동하지 않는 것 같습니다. rgdp_e_trans $ estimate %^% 1/5 rgdp_e_trans $ 추정 %^% 1 : 매트릭스가 아닙니다 .' – zlqs1985

+0

'str (rgdp_e_trans)'의 결과를'markovchainFit'으로 생성 한 후에 추가 할 수 있습니까? –

답변

4

as.matrix 부분 주석 :

[email protected] 

여기서 rgdp_e_transmarkovchainFit의 반환 값입니다. 실제로는 피팅 프로세스의 정보가 들어있는 목록입니다. $ 연산자를 사용하여 해당 목록의 estimates 항목에 액세스합니다. estimate 객체는 정식 S4 클래스에서 가져온 것입니다 (예 : R에서 사용되는 객체 시스템에 대한 설명은 Hadley Wickham이 Advanced R 참조). 따라서 항목에 액세스하려면 $ 표준 대신 @ 연산자를 사용해야합니다 보다 일반적인 S3 객체의 경우

as.matrix(rgdp_e_trans)의 반환 값을 인쇄하는 경우 초기 접근법이 잘못되었을 때 즉시 확인해야합니다. 일반적으로 예기치 않은 결과가 발생하거나 새로운 유형의 객체로 작업하는 경우 인쇄 방법에 의존하는 대신 str 기능을 사용하여 객체의 구조를 검사하는 것이 좋습니다.

+0

그것은 작동합니다. 또한 (내가했던 것처럼) 필요할 경우를 대비하여 rgdp_e_trans $ estimate @ states를 통해 주 이름에 액세스 할 수도 있습니다. – Jefferson