能否用AR、MA、ARMA等统计模型分类时间序列?VAR模型输出异常
Hey there! I see exactly what's going on here—let's get your model to output those 0/1 labels you're expecting instead of floating-point numbers.
Why You're Getting Floating-Point Output
First, let's clarify: the VAR model from the vars package is a vector autoregressive regression model, designed to predict continuous numerical time series. It estimates the conditional mean of your status variable (which happens to be coded 0/1 in your data) based on past values of all variables in the model. That's why it outputs continuous floating-point predictions instead of discrete 0/1 labels—it's treating status as a continuous variable, not a binary classification target.
Quick Fix: Convert Predictions to Binary Labels with a Threshold
The simplest way to get your 0/1 labels is to apply a threshold to the continuous predictions. Since your status is 0 (no exercise) and 1 (exercising), a common starting point is a 0.5 threshold—adjust this based on your data's distribution or performance metrics like precision/recall.
Here's how to modify your existing code:
# After running your predict() call prd <- predict(model, n.ahead = 10, ci = 0.95, dumvar = NULL) # Extract the continuous forecast values for status status_fcst <- prd$fcst$status[, "fcst"] # Apply threshold to get binary labels (0/1) # Adjust the threshold (0.5 here) based on your data's behavior status_class <- ifelse(status_fcst >= 0.5, 1, 0) # View your classification results print(status_class)
Better Long-Term Solution: Use a Model Built for Discrete Time Series Classification
If you want a more robust approach (since VAR isn't designed for binary targets), consider models tailored to discrete state prediction, especially since your problem is a state-switching scenario (exercise vs. no exercise):
Hidden Markov Models (HMMs)
HMMs are perfect for modeling discrete states that switch over time, with continuous observations (your ax/ay/az data). Use thedepmixS4package to build one:library(depmixS4) # Build an HMM with 2 states (0 and 1) using your sensor variables as observations hmm_model <- depmix(list(ax ~ 1, ay ~ 1, az ~ 1), data = dt, nstates = 2, family = list(gaussian(), gaussian(), gaussian())) # Fit the model to your data hmm_fit <- fit(hmm_model) # Predict the hidden states (map state 1/2 to your 0/1 labels) hmm_predictions <- predict(hmm_fit) status_hmm_class <- ifelse(hmm_predictions$state == 1, 0, 1)Discrete Vector Autoregressive Models
For binary dependent variables, you can extend VAR with logit/probit links. Packages likemlogitorglmmTMBcan help build these models, treatingstatusas a binary outcome rather than continuous.
Final Notes
- Test different thresholds if you stick with the VAR + threshold approach—use training data to find the threshold that maximizes accuracy, precision, or recall for your use case.
- HMMs will likely perform better for your state-switching scenario, as they explicitly model the discrete nature of your exercise status.
内容的提问来源于stack exchange,提问作者messi

