-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e09085a
commit 65a1b7c
Showing
13 changed files
with
978 additions
and
513 deletions.
There are no files selected for viewing
Binary file not shown.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Hola Alfonso. | ||
|
||
Al final lo he conseguido hacer hoy. | ||
|
||
Adjunto todo: | ||
|
||
mEt.mat = matrices de los valores estimados con MLE con 10 filas y 10 columnas que corresponden a los siguientes estados: 21,22,23,24, ... ,29,30 | ||
mLt.mat = matrices lower confidence interval | ||
mUt.mat = matrices upper confidence interval | ||
MarkovChain-i.R: código para generar los objetos con R | ||
|
||
Más info sobre el paquete: https://cran.r-project.org/web/packages/markovchain/vignettes/an_introduction_to_markovchain_package.pdf | ||
|
||
Cualquier cosa, me dices. | ||
|
||
Saludos! |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
library("markovchain") | ||
|
||
mEt <- array(NA, c(10, 10, ncol(df))) | ||
mUt <- array(NA, c(10, 10, ncol(df))) | ||
mLt <- array(NA, c(10, 10, ncol(df))) | ||
'%!in%' <- function(x,y)!('%in%'(x,y)) | ||
coln=as.character(c(21:30)) | ||
|
||
f.fillMatrixAurora = function(matrix,coln, with=0){ | ||
colnmatrix=colnames(matrix) | ||
matrix=matrix[match(coln,colnmatrix),match(coln,colnmatrix)] | ||
colnames(matrix)=coln | ||
rownames(matrix) = coln | ||
matrix[is.na(matrix)] <- with | ||
return(matrix) | ||
} | ||
|
||
for(i in 1:ncol(df)){ | ||
print(i) | ||
x = as.numeric(unlist(df[,..i])) | ||
if(sum(!is.na(x))!=0){ | ||
y = fillNAgaps(x, firstBack=T) | ||
weatherFittedMLE <- markovchainFit(data = y, method = "mle", name = "weatherFittedMLE", confidencelevel = 0.95) | ||
mE = weatherFittedMLE$estimate | ||
mE = mE@transitionMatrix | ||
mU = weatherFittedMLE$upperEndpointMatrix | ||
mL = weatherFittedMLE$lowerEndpointMatrix | ||
if(ncol(mE)!=length(coln)){ | ||
print("Filling") | ||
mE = f.fillMatrixAurora(mE, coln) | ||
mU = f.fillMatrixAurora(mU, coln, with = -999) | ||
mL = f.fillMatrixAurora(mL, coln, with = -999) | ||
} | ||
|
||
mEt[,,i] = mE | ||
mUt[,,i] = mU | ||
mLt[,,i] = mL | ||
}else{ | ||
print("TOOODO NA") # se queda con NA | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
|
||
library("R.matlab") | ||
writeMat("mEt.mat",mEt= mEt) | ||
writeMat("mUt.mat",mUt= mUt) | ||
writeMat("mLt.mat",mLt= mLt) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
library("markovchain") | ||
|
||
weatherStates <- c("sunny", "cloudy", "rain") | ||
byRow <- TRUE | ||
weatherMatrix <- matrix(data = c(0.70, 0.2, 0.1,0.3, 0.4, 0.3, | ||
0.2, 0.45, 0.35), byrow = byRow, nrow = 3, | ||
dimnames = list(weatherStates, weatherStates)) | ||
mcWeather <- new("markovchain", states = weatherStates, byrow = byRow, | ||
transitionMatrix = weatherMatrix, name = "Weather") | ||
|
||
weathersOfDays <- rmarkovchain(n = 365, object = mcWeather, t0 = "sunny") | ||
|
||
|
||
|
||
|
||
weatherFittedLAPLACE <- markovchainFit(data = weathersOfDays, | ||
method = "laplace", laplacian = 0.01, | ||
name = "Weather LAPLACE") | ||
weatherFittedLAPLACE$estimate | ||
weatherFittedLAPLACE$confidenceInterval | ||
|
||
|
||
|
||
|
||
|
||
weatherFittedBOOT <- markovchainFit(data = weathersOfDays, method = "bootstrap", nboot = 20) | ||
weatherFittedBOOT$estimate | ||
weatherFittedBOOT$confidenceInterval | ||
|
||
|
||
|
||
weatherFittedMLE <- markovchainFit(data = weathersOfDays, method = "mle", name = "weatherFittedMLE", confidencelevel = 0.95) | ||
weatherFittedMLE$estimate | ||
weatherFittedMLE$upperEndpointMatrix | ||
weatherFittedMLE$lowerEndpointMatrix | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
################ | ||
library("data.table") | ||
df <- fread("TsetCleanV7.csv", sep=";") | ||
|
||
|
||
fillNAgaps <- function(x, firstBack=FALSE) { | ||
## NA's in a vector or factor are replaced with last non-NA values | ||
## If firstBack is TRUE, it will fill in leading NA's with the first | ||
## non-NA value. If FALSE, it will not change leading NA's. | ||
|
||
# If it's a factor, store the level labels and convert to integer | ||
lvls <- NULL | ||
if (is.factor(x)) { | ||
lvls <- levels(x) | ||
x <- as.integer(x) | ||
} | ||
|
||
goodIdx <- !is.na(x) | ||
|
||
# These are the non-NA values from x only | ||
# Add a leading NA or take the first good value, depending on firstBack | ||
if (firstBack) goodVals <- c(x[goodIdx][1], x[goodIdx]) | ||
else goodVals <- c(NA, x[goodIdx]) | ||
|
||
# Fill the indices of the output vector with the indices pulled from | ||
# these offsets of goodVals. Add 1 to avoid indexing to zero. | ||
fillIdx <- cumsum(goodIdx)+1 | ||
|
||
x <- goodVals[fillIdx] | ||
|
||
# If it was originally a factor, convert it back | ||
if (!is.null(lvls)) { | ||
x <- factor(x, levels=seq_along(lvls), labels=lvls) | ||
} | ||
|
||
x | ||
} | ||
|
||
x = as.numeric(unlist(df[,1])) | ||
y = fillNAgaps(x, firstBack=T) | ||
|
||
weatherFittedMLE <- markovchainFit(data = y, method = "mle", name = "weatherFittedMLE", confidencelevel = 0.95) | ||
weatherFittedMLE$estimate | ||
weatherFittedMLE$upperEndpointMatrix | ||
weatherFittedMLE$lowerEndpointMatrix | ||
|
||
|
||
weatherFittedBOOT<- markovchainFit(data = y, method = "bootstrap", nboot = 20) | ||
weatherFittedBOOT$estimate | ||
weatherFittedBOOT$confidenceInterval | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
mEt <- array(NA, c(10, 10, ncol(df))) | ||
mUt <- array(NA, c(10, 10, ncol(df))) | ||
mLt <- array(NA, c(10, 10, ncol(df))) | ||
'%!in%' <- function(x,y)!('%in%'(x,y)) | ||
coln=as.character(c(21:30)) | ||
|
||
f.fillMatrixAurora = function(matrix,coln, with=0){ | ||
colnmatrix=colnames(matrix) | ||
matrix=matrix[match(coln,colnmatrix),match(coln,colnmatrix)] | ||
colnames(matrix)=coln | ||
rownames(matrix) = coln | ||
matrix[is.na(matrix)] <- with | ||
return(matrix) | ||
} | ||
|
||
for(i in 1:ncol(df)){ | ||
print(i) | ||
x = as.numeric(unlist(df[,..i])) | ||
if(sum(!is.na(x))!=0){ | ||
y = fillNAgaps(x, firstBack=T) | ||
weatherFittedMLE <- markovchainFit(data = y, method = "mle", name = "weatherFittedMLE", confidencelevel = 0.95) | ||
mE = weatherFittedMLE$estimate | ||
mE = mE@transitionMatrix | ||
mU = weatherFittedMLE$upperEndpointMatrix | ||
mL = weatherFittedMLE$lowerEndpointMatrix | ||
if(ncol(mE)!=length(coln)){ | ||
print("Filling") | ||
mE = f.fillMatrixAurora(mE, coln) | ||
mU = f.fillMatrixAurora(mU, coln, with = -999) | ||
mL = f.fillMatrixAurora(mL, coln, with = -999) | ||
} | ||
|
||
mEt[,,i] = mE | ||
mUt[,,i] = mU | ||
mLt[,,i] = mL | ||
}else{ | ||
print("TOOODO NA") # se queda con NA | ||
} | ||
|
||
|
||
} | ||
|
||
|
||
|
||
library("R.matlab") | ||
writeMat("mEt.mat",mEt= mEt) | ||
writeMat("mUt.mat",mUt= mUt) | ||
writeMat("mLt.mat",mLt= mLt) | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"","cons","income","price","temp" | ||
"1",0.386,78,0.27,41 | ||
"2",0.374,79,0.282,56 | ||
"3",0.393,81,0.277,63 | ||
"4",0.425,80,0.28,68 | ||
"5",0.406,76,0.272,69 | ||
"6",0.344,78,0.262,65 | ||
"7",0.327,82,0.275,61 | ||
"8",0.288,79,0.267,47 | ||
"9",0.269,76,0.265,32 | ||
"10",0.256,79,0.277,24 | ||
"11",0.286,82,0.282,28 | ||
"12",0.298,85,0.27,26 | ||
"13",0.329,86,0.272,32 | ||
"14",0.318,83,0.287,40 | ||
"15",0.381,84,0.277,55 | ||
"16",0.381,82,0.287,63 | ||
"17",0.47,80,0.28,72 | ||
"18",0.443,78,0.277,72 | ||
"19",0.386,84,0.277,67 | ||
"20",0.342,86,0.277,60 | ||
"21",0.319,85,0.292,44 | ||
"22",0.307,87,0.287,40 | ||
"23",0.284,94,0.277,32 | ||
"24",0.326,92,0.285,27 | ||
"25",0.309,95,0.282,28 | ||
"26",0.359,96,0.265,33 | ||
"27",0.376,94,0.265,41 | ||
"28",0.416,96,0.265,52 | ||
"29",0.437,91,0.268,64 | ||
"30",0.548,90,0.26,71 |
Oops, something went wrong.