plot optimized muso
This commit is contained in:
parent
760bbaef9a
commit
4b1c5fe5a3
@ -44,6 +44,7 @@ optiMuso <- function(measuredData, parameters = NULL, startDate,
|
||||
modelVar = 3009,
|
||||
postProcString = NULL)
|
||||
{
|
||||
mdata <- measuredData
|
||||
dataCol <- grep(dataVar, colnames(measuredData))
|
||||
|
||||
if(is.null(parameters)){
|
||||
@ -104,6 +105,7 @@ optiMuso <- function(measuredData, parameters = NULL, startDate,
|
||||
randValues <- randVals[[2]]
|
||||
settings$calibrationPar <- randVals[[1]]
|
||||
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
|
||||
rmse <- numeric(iterations + 1)
|
||||
@ -149,12 +151,21 @@ optiMuso <- function(measuredData, parameters = NULL, startDate,
|
||||
preservedCalib <- preservedCalib[-1,]
|
||||
dontInclude <-c((ncol(preservedCalib)-1),ncol(preservedCalib))
|
||||
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
|
||||
#' @importFrom lubridate leap_year
|
||||
#' @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){
|
||||
if((is.null(startDate) | is.null(endDate))){
|
||||
stop("If your date is continuous, you have to provide both startDate and endDate. ")
|
||||
}
|
||||
startDate <- as.Date(startDate, format = formatString)
|
||||
endDate <- as.Date(endDate, format = formatString)
|
||||
mdata <- as.data.frame(mdata)
|
||||
}
|
||||
|
||||
if(is.null(modellSettings)){
|
||||
modellSettings <- setupMuso()
|
||||
}
|
||||
|
||||
mdata <- as.data.frame(mdata)
|
||||
|
||||
if(continious){
|
||||
dates <- seq(startDate, to = endDate, by= "day")
|
||||
} 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
|
||||
#' @export
|
||||
plotMusoWithData <- function(mdata, plotName=NULL,
|
||||
startDate, endDate,
|
||||
colour=c("black","blue"),dataVar, modelVar, settings = setupMuso(), silent = TRUE, continious = TRUE){
|
||||
startDate = NULL, endDate = NULL,
|
||||
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))
|
||||
selVar <- grep(modelVar,(settings$dailyVarCodes))+4
|
||||
@ -261,21 +265,33 @@ plotMusoWithData <- function(mdata, plotName=NULL,
|
||||
modellSettings = settings,
|
||||
startDate = startDate,
|
||||
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
|
||||
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")
|
||||
selVarName <- colnames(baseData)[selVar]
|
||||
if(!all.equal(colnames(baseData),unique(colnames(baseData)))){
|
||||
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))
|
||||
}
|
||||
|
||||
mesData<-cbind.data.frame(baseData[,1],mesData)
|
||||
colnames(mesData) <- c("date", "measured")
|
||||
p <- baseData %>%
|
||||
ggplot(aes_string("date",selVarName)) +
|
||||
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"))+
|
||||
theme(axis.title.x = element_blank())
|
||||
if(!is.null(plotName)){
|
||||
|
||||
Loading…
Reference in New Issue
Block a user