plot optimized muso
This commit is contained in:
parent
760bbaef9a
commit
4b1c5fe5a3
@ -44,6 +44,7 @@ optiMuso <- function(measuredData, parameters = NULL, startDate,
|
|||||||
modelVar = 3009,
|
modelVar = 3009,
|
||||||
postProcString = NULL)
|
postProcString = NULL)
|
||||||
{
|
{
|
||||||
|
mdata <- measuredData
|
||||||
dataCol <- grep(dataVar, colnames(measuredData))
|
dataCol <- grep(dataVar, colnames(measuredData))
|
||||||
|
|
||||||
if(is.null(parameters)){
|
if(is.null(parameters)){
|
||||||
@ -104,6 +105,7 @@ optiMuso <- function(measuredData, parameters = NULL, startDate,
|
|||||||
randValues <- randVals[[2]]
|
randValues <- randVals[[2]]
|
||||||
settings$calibrationPar <- randVals[[1]]
|
settings$calibrationPar <- randVals[[1]]
|
||||||
list2env(alignData(measuredData,dataCol = dataCol,modellSettings = settings,startDate = startDate,endDate = endDate,leapYear = leapYearHandling, continious = continious),envir=environment())
|
list2env(alignData(measuredData,dataCol = dataCol,modellSettings = settings,startDate = startDate,endDate = endDate,leapYear = leapYearHandling, continious = continious),envir=environment())
|
||||||
|
## modIndex and measuredData are created.
|
||||||
|
|
||||||
modellOut <- numeric(iterations + 1) # single variable solution
|
modellOut <- numeric(iterations + 1) # single variable solution
|
||||||
rmse <- numeric(iterations + 1)
|
rmse <- numeric(iterations + 1)
|
||||||
@ -149,12 +151,21 @@ optiMuso <- function(measuredData, parameters = NULL, startDate,
|
|||||||
preservedCalib <- preservedCalib[-1,]
|
preservedCalib <- preservedCalib[-1,]
|
||||||
dontInclude <-c((ncol(preservedCalib)-1),ncol(preservedCalib))
|
dontInclude <-c((ncol(preservedCalib)-1),ncol(preservedCalib))
|
||||||
for(i in seq_along(colnames(preservedCalib)[-dontInclude])){
|
for(i in seq_along(colnames(preservedCalib)[-dontInclude])){
|
||||||
p[[i]] <- ggplot(as.data.frame(preservedCalib),aes_string(colnames(preservedCalib)[i],"likelihood"))+geom_point(shape='.',size=1,alpha=0.8)
|
p[[i]] <- ggplot(as.data.frame(preservedCalib),aes_string(colnames(preservedCalib)[i],"likelihood")) +
|
||||||
|
geom_point(shape='.',size=1,alpha=0.8)
|
||||||
}
|
}
|
||||||
|
|
||||||
ggsave(plotName,grid.arrange(grobs = p, ncol = floor(sqrt(ncol(preservedCalib)-1))),dpi = 3000)
|
ggsave(plotName,grid.arrange(grobs = p, ncol = floor(sqrt(ncol(preservedCalib)-1))),dpi = 300)
|
||||||
|
maxLikelihoodPlace <- which(preservedCalib[,"likelihood"]==max(preservedCalib[,"likelihood"],na.rm = TRUE))
|
||||||
|
resPlot <- plotMusoWithData(mdata = mdata, startDate = startDate, endDate = endDate,
|
||||||
|
dataVar = dataVar, modelVar = modelVar, settings = settings, continious = continious) +
|
||||||
|
plotMuso(settings = settings, parameters = randValues[maxLikelihoodPlace,],
|
||||||
|
postProcString = postProcString, skipSpinup = FALSE, variable = colNumb, layerPlot = TRUE, colour = "green")
|
||||||
|
|
||||||
return(preservedCalib[preservedCalib[,"likelihood"]==max(preservedCalib[,"likelihood"],na.rm = TRUE),])
|
print(resPlot)
|
||||||
|
tempEpc <- paste0(tools::file_path_sans_ext(basename(settings$epcInput[2])),"-tmp.",tools::file_ext(settings$epcInput[2]))
|
||||||
|
file.rename(tempEpc, "optimizedEpc.epc")
|
||||||
|
return(preservedCalib[maxLikelihoodPlace,])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -59,17 +59,22 @@ musoDate <- function(startYear, endYears = NULL, numYears, combined = TRUE, leap
|
|||||||
#' This function align the data to the model and the model to the data
|
#' This function align the data to the model and the model to the data
|
||||||
#' @importFrom lubridate leap_year
|
#' @importFrom lubridate leap_year
|
||||||
#' @keywords internal
|
#' @keywords internal
|
||||||
alignData <- function(mdata, dataCol, modellSettings = NULL, startDate=NULL, endDate=NULL, formatString = "%Y-%m-%d", leapYear = TRUE, continious = TRUE){
|
alignData <- function(mdata, dataCol, modellSettings = NULL, startDate=NULL, endDate=NULL, formatString = "%Y-%m-%d", leapYear = TRUE, continious = FALSE){
|
||||||
|
|
||||||
|
if(continious){
|
||||||
startDate <- as.Date(startDate, format = formatString)
|
if((is.null(startDate) | is.null(endDate))){
|
||||||
endDate <- as.Date(endDate, format = formatString)
|
stop("If your date is continuous, you have to provide both startDate and endDate. ")
|
||||||
mdata <- as.data.frame(mdata)
|
}
|
||||||
|
startDate <- as.Date(startDate, format = formatString)
|
||||||
|
endDate <- as.Date(endDate, format = formatString)
|
||||||
|
}
|
||||||
|
|
||||||
if(is.null(modellSettings)){
|
if(is.null(modellSettings)){
|
||||||
modellSettings <- setupMuso()
|
modellSettings <- setupMuso()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mdata <- as.data.frame(mdata)
|
||||||
|
|
||||||
if(continious){
|
if(continious){
|
||||||
dates <- seq(startDate, to = endDate, by= "day")
|
dates <- seq(startDate, to = endDate, by= "day")
|
||||||
} else{
|
} else{
|
||||||
|
|||||||
@ -251,8 +251,12 @@ plotMuso <- function(settings = NULL, variable = 1,
|
|||||||
#' @importFrom ggplot2 ggplot geom_line geom_point aes aes_string labs theme element_blank
|
#' @importFrom ggplot2 ggplot geom_line geom_point aes aes_string labs theme element_blank
|
||||||
#' @export
|
#' @export
|
||||||
plotMusoWithData <- function(mdata, plotName=NULL,
|
plotMusoWithData <- function(mdata, plotName=NULL,
|
||||||
startDate, endDate,
|
startDate = NULL, endDate = NULL,
|
||||||
colour=c("black","blue"),dataVar, modelVar, settings = setupMuso(), silent = TRUE, continious = TRUE){
|
colour=c("black","blue"), dataVar, modelVar, settings = setupMuso(), silent = TRUE, continious = FALSE){
|
||||||
|
|
||||||
|
if(continious & (is.null(startDate) | is.null(endDate))){
|
||||||
|
stop("If your date is continuous, you have to provide both startDate and endDate. ")
|
||||||
|
}
|
||||||
|
|
||||||
dataCol<- grep(paste0("^",dataVar,"$"), colnames(mdata))
|
dataCol<- grep(paste0("^",dataVar,"$"), colnames(mdata))
|
||||||
selVar <- grep(modelVar,(settings$dailyVarCodes))+4
|
selVar <- grep(modelVar,(settings$dailyVarCodes))+4
|
||||||
@ -261,21 +265,33 @@ plotMusoWithData <- function(mdata, plotName=NULL,
|
|||||||
modellSettings = settings,
|
modellSettings = settings,
|
||||||
startDate = startDate,
|
startDate = startDate,
|
||||||
endDate = endDate, leapYear = FALSE, continious = continious),envir=environment())
|
endDate = endDate, leapYear = FALSE, continious = continious),envir=environment())
|
||||||
|
mesData <- numeric(settings$numYears*365)
|
||||||
|
k <- 1
|
||||||
|
for(i in seq(mesData)){
|
||||||
|
if(i %in% modIndex){
|
||||||
|
mesData[i] <- measuredData[k]
|
||||||
|
k <- k + 1
|
||||||
|
} else {
|
||||||
|
mesData[i] <- NA
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rm(k)
|
||||||
|
# modIndex and measuredData are created.
|
||||||
## measuredData is created
|
## measuredData is created
|
||||||
baseData <- calibMuso(settings = settings, silent = silent, prettyOut = TRUE)[modIndex,]
|
## baseData <- calibMuso(settings = settings, silent = silent, prettyOut = TRUE)[modIndex,]
|
||||||
|
baseData <- calibMuso(settings = settings, silent = silent, prettyOut = TRUE)
|
||||||
baseData[,1] <- as.Date(baseData[,1],format = "%d.%m.%Y")
|
baseData[,1] <- as.Date(baseData[,1],format = "%d.%m.%Y")
|
||||||
selVarName <- colnames(baseData)[selVar]
|
selVarName <- colnames(baseData)[selVar]
|
||||||
if(!all.equal(colnames(baseData),unique(colnames(baseData)))){
|
if(!all.equal(colnames(baseData),unique(colnames(baseData)))){
|
||||||
notUnique <- setdiff((unlist(settings$dailyVarCodes)),unique(unlist(settings$dailyVarCodes)))
|
notUnique <- setdiff((unlist(settings$dailyVarCodes)),unique(unlist(settings$dailyVarCodes)))
|
||||||
stop(paste0("Error: daily output variable list in the ini file must contain unique numbers. Check your ini files! Not unique codes: ",notUnique))
|
stop(paste0("Error: daily output variable list in the ini file must contain unique numbers. Check your ini files! Not unique codes: ",notUnique))
|
||||||
}
|
}
|
||||||
|
mesData<-cbind.data.frame(baseData[,1],mesData)
|
||||||
|
colnames(mesData) <- c("date", "measured")
|
||||||
p <- baseData %>%
|
p <- baseData %>%
|
||||||
ggplot(aes_string("date",selVarName)) +
|
ggplot(aes_string("date",selVarName)) +
|
||||||
geom_line(colour=colour[1]) +
|
geom_line(colour=colour[1]) +
|
||||||
geom_point(colour=colour[2], aes(date,measuredData)) +
|
geom_point(data = mesData, colour=colour[2], aes(date,measured)) +
|
||||||
labs(y = paste0(selVarName,"_measured"))+
|
labs(y = paste0(selVarName,"_measured"))+
|
||||||
theme(axis.title.x = element_blank())
|
theme(axis.title.x = element_blank())
|
||||||
if(!is.null(plotName)){
|
if(!is.null(plotName)){
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user