R/create_dataset.R

create_data_df <- function(data, flux, air, tau) {
  # Raw data
  # Check whether the format is like cwall_east or like cwall_north
  if ("T_in" %in% names(data)) {
    Qin <- data[, "Q_in"]
    Qout <- data[, "Q_out"]
    Ti <- data[, "T_in"]
    Te <- data[, "T_out"]
  } else {
    Qin <- data[, "Q_in"]
    Qout <- data[, "Q_out"]
    if (air) {
      Ti <- data[, "TA_int"]
      Te <- data[, "TA_ext"]
    } else {
      Ti <- data[, "TS_int"]
      Te <- data[, "TS_ext"]
    }
  }
  # Sample size
  n <- nrow(data)
  # Data for model
  # Qin
  Qinp <- Qin[2:n]
  Qinp1 <- Qin[1:(n - 1)]
  # Qout
  Qoutp <- Qout[2:n]
  Qoutp1 <- Qout[1:(n - 1)]
  the_time <- data[2:n, "Time"]
  # x1, x2, x3
  x1 <- (Ti[2:n] + Ti[1:(n - 1)]) / 2 - (Te[2:n] + Te[1:(n - 1)]) / 2
  x2 <- Ti[2:n] - Ti[1:(n - 1)]
  x3 <- Te[2:n] - Te[1:(n - 1)]
  # Deal with missing values
  # Create a matrix of the data involved in the model
  if (flux == "in") {
    temp_data <- cbind(Qinp, x1, x2, Qinp1, the_time)
    temp_data <- na.omit(temp_data)
    Qinp <- temp_data[, "Qinp"]
    x1 <- temp_data[, "x1"]
    x2 <- temp_data[, "x2"]
    the_time <- as.numeric(temp_data[, "the_time"])
    Qinp1 <- temp_data[, "Qinp1"]
    data_matrix <- cbind(Qinp, x1, x2, Qinp1, tau)
    data_df <- data.frame(Qinp = Qinp, x1 = x1, x2 = x2, Qinp1 = Qinp1,
                          tau = tau, the_time = the_time)
  } else if (flux == "out") {
    temp_data <- cbind(Qoutp, x1, x3, Qoutp1, the_time)
    temp_data <- na.omit(temp_data)
    # Note the negation of the Qout values in the data file
    Qoutp <- -temp_data[, "Qoutp"]
    Qoutp1 <- -temp_data[, "Qoutp1"]
    x1 <- temp_data[, "x1"]
    x3 <- temp_data[, "x3"]
    the_time <- as.numeric(temp_data[, "the_time"])
    data_df <- data.frame(Qoutp = Qoutp, x1 = x1, x3 = -x3, Qoutp1 = Qoutp1,
                          tau = tau, the_time = the_time)
  } else {
#    temp_data <- cbind(Qinp, Qoutp, x1, x2, x3, Qinp1, Qoutp1, the_time)
    temp_data <- data.frame(Qinp, Qoutp, x1, x2, x3, Qinp1, Qoutp1, the_time)
    temp_data <- na.omit(temp_data)
    Qinp <- temp_data[, "Qinp"]
    Qinp1 <- temp_data[, "Qinp1"]
    # Note the negation of the Qout values in the data file
    Qoutp <- -temp_data[, "Qoutp"]
    Qoutp1 <- -temp_data[, "Qoutp1"]
    x1 <- temp_data[, "x1"]
    x2 <- temp_data[, "x2"]
    x3 <- temp_data[, "x3"]
#    the_time <- factor(temp_data[, "the_time"])
#    the_time <- as.numeric(temp_data[, "the_time"])
    the_time <- temp_data[, "the_time"]
    Qsp <- c(Qinp, Qoutp)
    Qsp1 <- c(Qinp1, Qoutp1)
    n <- nrow(temp_data)
    in_out <- factor(c(rep("inside", n), rep("outside", n)))
    zeros <- rep(0, n)
    the_time <- c(the_time, the_time)
    #    class(the_time) <- c("ordered", "factor")
    data_df <- data.frame(Qsp = Qsp, x1 = c(x1, x1), x23 = c(x2, -x3),
                          Qsp1 = Qsp1, in_out = in_out, tau = tau,
                          the_time = the_time)
#    new_data_df <- nlme::groupedData(Qsp ~ 1 | the_time, data = data_df)
#    print(head(new_data_df))
#    data_df <- new_data_df
  }
  return(data_df)
}

two_tm_create_data_df <- function(data, air, tau) {
  # Raw data
  # Check whether the format is like cwall_east or like cwall_north
  if ("T_in" %in% names(data)) {
    Qin <- data[, "Q_in"]
    Qout <- data[, "Q_out"]
    Ti <- data[, "T_in"]
    Te <- data[, "T_out"]
  } else {
    Qin <- data[, "Q_in"]
    Qout <- data[, "Q_out"]
    if (air) {
      Ti <- data[, "TA_int"]
      Te <- data[, "TA_ext"]
    } else {
      Ti <- data[, "TS_int"]
      Te <- data[, "TS_ext"]
    }
  }
  # Sample size
  n <- nrow(data)
  # Data for model
  # Qin
  Qinp <- Qin[2:n]
  Qinp1 <- Qin[1:(n - 1)]
  # Qout
  Qoutp <- Qout[2:n]
  Qoutp1 <- Qout[1:(n - 1)]
  the_time <- data[2:n, "Time"]
  # x1, x2, x3
  x1 <- (Ti[2:n] + Ti[1:(n - 1)]) / 2 - (Te[2:n] + Te[1:(n - 1)]) / 2
  x2 <- Ti[2:n] - Ti[1:(n - 1)]
  x3 <- Te[2:n] - Te[1:(n - 1)]
  # Deal with missing values
  # Create a matrix of the data involved in the model
#  temp_data <- cbind(Qinp, Qoutp, x1, x2, x3, Qinp1, Qoutp1, the_time)
  temp_data <- data.frame(Qinp, Qoutp, x1, x2, x3, Qinp1, Qoutp1, the_time)
  temp_data <- na.omit(temp_data)
  Qinp <- temp_data[, "Qinp"]
  Qinp1 <- temp_data[, "Qinp1"]
  # Note the negation of the Qout values in the data file
  Qoutp <- -temp_data[, "Qoutp"]
  Qoutp1 <- -temp_data[, "Qoutp1"]
  x1 <- temp_data[, "x1"]
  x2 <- temp_data[, "x2"]
  x3 <- temp_data[, "x3"]
  #    the_time <- factor(temp_data[, "the_time"])
  #    the_time <- as.numeric(temp_data[, "the_time"])
  the_time <- temp_data[, "the_time"]
  Qp <- c(Qinp, Qoutp)
  Qp1 <- c(Qinp1, Qoutp1)
  n <- nrow(temp_data)
  in_out <- factor(c(rep("inside", n), rep("outside", n)))
  zeros <- rep(0, n)
  the_time <- c(the_time, the_time)
  #    class(the_time) <- c("ordered", "factor")
  data_df <- data.frame(Qp = Qp, x1 = c(x1, x1), x2 = c(x2, x2),
                        x3 = c(x3, x3), Qp1 = Qp1, in_out = in_out,
                        tau = tau, the_time = the_time)
  return(data_df)
}

one_tm_sun_create_data_df <- function(data, air, tau) {
  # Raw data
  # Check that Q_sun is present
  if (!("Q_sun" %in% names(data))) {
    stop("data must contain the variable Q_sun")
  }
  # Check whether the format is like cwall_east or like cwall_north
  if ("T_in" %in% names(data)) {
    Qin <- data[, "Q_in"]
    Qout <- data[, "Q_out"]
    Ti <- data[, "T_in"]
    Te <- data[, "T_out"]
  } else {
    Qin <- data[, "Q_in"]
    Qout <- data[, "Q_out"]
    if (air) {
      Ti <- data[, "TA_int"]
      Te <- data[, "TA_ext"]
    } else {
      Ti <- data[, "TS_int"]
      Te <- data[, "TS_ext"]
    }
  }
  # Sample size
  n <- nrow(data)
  # Data for model
  # Qin
  Qinp <- Qin[2:n]
  Qinp1 <- Qin[1:(n - 1)]
  # Qout
  Qoutp <- Qout[2:n]
  Qoutp1 <- Qout[1:(n - 1)]
  the_time <- data[2:n, "Time"]
  # x1, x2, x3
  x1 <- (Ti[2:n] + Ti[1:(n - 1)]) / 2 - (Te[2:n] + Te[1:(n - 1)]) / 2
  x2 <- Ti[2:n] - Ti[1:(n - 1)]
  x3 <- Te[2:n] - Te[1:(n - 1)]
  # x4, x5 (mean of Qsunp and Qsunp1, Qsunp - Qsunp1)
  Qsunp <- data[2:n, "Q_sun"]
  Qsunp1 <- data[1:(n - 1), "Q_sun"]
  x4 <- (Qsunp + Qsunp1) / 2
  x5 <- Qsunp - Qsunp1
  # Deal with missing values
  # Create a matrix of the data involved in the model
#  temp_data <- cbind(Qinp, Qoutp, x1, x2, x3, x4, x5, Qinp1, Qoutp1, the_time)
  temp_data <- data.frame(Qinp, Qoutp, x1, x2, x3, x4, x5, Qinp1, Qoutp1,
                          the_time)
  temp_data <- na.omit(temp_data)
  Qinp <- temp_data[, "Qinp"]
  Qinp1 <- temp_data[, "Qinp1"]
  # Note the negation of the Qout values in the data file
  Qoutp <- -temp_data[, "Qoutp"]
  Qoutp1 <- -temp_data[, "Qoutp1"]
  x1 <- temp_data[, "x1"]
  x2 <- temp_data[, "x2"]
  x3 <- temp_data[, "x3"]
  x4 <- temp_data[, "x4"]
  x5 <- temp_data[, "x5"]
  #    the_time <- factor(temp_data[, "the_time"])
  #    the_time <- as.numeric(temp_data[, "the_time"])
  the_time <- temp_data[, "the_time"]
  Qp <- c(Qinp, Qoutp)
  Qp1 <- c(Qinp1, Qoutp1)
  n <- nrow(temp_data)
  in_out <- factor(c(rep("inside", n), rep("outside", n)))
  zeros <- rep(0, n)
  the_time <- c(the_time, the_time)
  #    class(the_time) <- c("ordered", "factor")
  data_df <- data.frame(Qp = Qp, x1 = c(x1, x1), x2 = c(x2, x2),
                        x3 = c(x3, x3), x4 = c(x4, x4), x5 = c(x5, x5),
                        Qp1 = Qp1, in_out = in_out, tau = tau,
                        the_time = the_time)
  return(data_df)
}
paulnorthrop/walls documentation built on May 15, 2019, 10:02 p.m.