먼저 마코프 천이 행렬을 계산하고 그 지수를 취합니다. 첫 번째 목표를 달성하기 위해 나는 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
'rgdp_e_trans $ estimate %^% 1/5'을 사용해보세요. –
작동하지 않는 것 같습니다. rgdp_e_trans $ estimate %^% 1/5 rgdp_e_trans $ 추정 %^% 1 : 매트릭스가 아닙니다 .' – zlqs1985
'str (rgdp_e_trans)'의 결과를'markovchainFit'으로 생성 한 후에 추가 할 수 있습니까? –