# Time series analysis for predicting a binary outcome

I'm fairly new to time series analysis. I want to analyze two series of variables in a span of time to predict a binary outcome.

For example i collect data over time at my home of two variables:

VarA the temperature over time

VarB the humidity over time

Then at 12:00 am i stop collecting this data and i see at 4:00 pm if it rains or not.

With a big dataset i want to predict given the time seres data collected till 12:00 am if it will rain at 4:00 pm. How can i accomplish this?

I was thinking about a k-nearest neighbors regression type analysis but i'm not sure how can i implement this.

EDIT: Here a fictional data set, i don't have already the data because i'm still defining the details. I want to know if studying the two time series (the difference from the start or other parameters) is there a way to predict the outcome of the day • Maybe I can help you if you share a small sample of your data. From what I understand you have many time series of temperature and humidity and for each one you want to predict if it's going to rain, right? This is binary classification problem, but in order to apply KNN in R you need to split your data into train and test set. – Regis A. Ely Jan 29 '16 at 14:56

There are many models that you can use for binary classification problems, such as logistic regressions, linear discriminant analysis, K-nearest-neighbours, trees, random forest, support vector machines, etc. You can review some of these methods in the book An Introduction to Statistical Learning from James, G. et al, specially chapter 4.

If you want to implement a K-nearest-neighbour in R with your data you first need to make sure you have a training set, a test set and classifications for the training set. Then you can do it with the class package. Below you can see an example. I am generating 8 random observations of temperatures and relative humidity for 1000 days and then classifying as "rain" or "sun" using a simple rule (I suppose that always rains when the mean of humidity in each day is higher than 52). Once I generate the data I split it into a train set (750 first observations) and a test set (the other 250). Then I generate predictions for the test set using the classification of the first 750 observations and check the results:

library(class)

## Generate random temp and hum for 1000 days (rains when mean(hum) > 52)
input <- matrix(0, 1000, 16)
output <- character(1000)
for (i in 1:1000){
input[i,1:8] <- sample(20:35, 8, replace = TRUE)
input[i,9:16] <- sample(40:60, 8, replace = TRUE)
if (mean(input[i,9:16]) > 52) {
output[i] <- "rain"
} else {
output[i] <- "sun"
}
}
data <- data.frame(input, as.factor(output))
names(data) <- c(paste("temp_", 1:8), paste("hum_", 1:8), "output")

## Split data into train, test and classification
train <- data[1:750, 1:16]
test <- data[751:1000, 1:16]
cl <- data[1:750, 17]

## Run K-Nearest Neighbour with k=1 to predict last 250 obs
(kn <- knn(train, test, cl, k = 1))
(success <- sum(data[751:1000, 17] == kn))

• so there are no way to include in the analysis the variability of the time series values? I have to calculate a parameter such as the mean value for example and fit it in the model? – GGA Jan 29 '16 at 20:25
• No, i'm using the mean only to determine when it rains or not, in order to generate the sample series. The knn function will then use all information to make the forecast. – Regis A. Ely Jan 29 '16 at 20:31
• In a real world example you will have that data. – Regis A. Ely Jan 29 '16 at 20:32

There are many models that you can use for binary classification problems quandldata = Quandl.dataset.get("NSE/OIL", list(rows=5))

head(quandldata) Date Open High Low Last 1 2019-01-04 172.05 174.95 172.05 174.55 2 2019-01-03 172.80 175.70 171.50 172.00 3 2019-01-02 175.80 176.20 171.00 172.35 4 2019-01-01 175.00 176.40 174.15 175.15 5 2018-12-31 178.10 179.00 174.35 175.00 Close Total Trade Quantity 1 174.55 431122 2 172.00 698190 3 172.40 722532 4 175.75 381570 5 174.80 761462 Turnover (Lacs) 1 749.99 2 1212.34 3 1251.85 4 669.16 5 1343.75 more

• You need to elaborate on your answer. – Michael R. Chernick Aug 13 '19 at 15:48