[chronojump/shortsLibrary: 142/146] Using shorts library for better calculations




commit 41dcad94b685156ae312792373494fdaaf47081d
Author: Xavier Padullés <testing chronojump org>
Date:   Thu Dec 17 20:31:25 2020 +0100

    Using shorts library for better calculations

 r-scripts/sprintPhotocells.R                   | 58 ++++++++++++++++++++++--
 r-scripts/tests/shortsLibrary/learningShorts.R | 61 +++++++++++++++++++++-----
 2 files changed, 106 insertions(+), 13 deletions(-)
---
diff --git a/r-scripts/sprintPhotocells.R b/r-scripts/sprintPhotocells.R
index 26222f30..af705dcd 100644
--- a/r-scripts/sprintPhotocells.R
+++ b/r-scripts/sprintPhotocells.R
@@ -89,17 +89,23 @@ getSprintFromPhotocell <- function(positions, splitTimes, noise=0)
                 Vmax = model$Vmax
         } else if (length(positions) == 4){
                 require(shorts)
-                model <- model_using_splits(positions[2:length(positions)], splitTimes[2:length(positions)])
+                model <- getModelWithOptimalTimeCorrection(data.frame(
+                        distance = positions[2:length(positions)],
+                        time= splitTimes[2:length(positions)]))
                 K = 1/ model$parameters$TAU
                 Vmax = model$parameters$MSS
         } else if (length(positions) == 5){
                 require(shorts)
-                model <- model_using_splits_with_time_correction(positions[2:length(positions)], 
splitTimes[2:length(positions)])
+                model <- model_using_splits_with_time_correction(data.frame(
+                        distance = positions[2:length(positions)],
+                        time= splitTimes[2:length(positions)]))
                 K = 1/ model$parameters$TAU
                 Vmax = model$parameters$MSS
         }else if (length(positions) >= 6){
                 require(shorts)
-                model <- model_using_splits_with_corrections(positions[2:length(positions)], 
splitTimes[2:length(positions)])
+                model <- model_using_splits_with_corrections(data.frame(
+                        distance = positions[2:length(positions)],
+                        time= splitTimes[2:length(positions)]))
                 K = 1/ model$parameters$TAU
                 Vmax = model$parameters$MSS
         }
@@ -144,6 +150,52 @@ getSprintFrom2SplitTimes <- function(x1, x2, t1, t2, tolerance = 0.0001, initK =
         return(list(K = K, Vmax = Vmax))
 }
 
+# get the model adjusting the time_correction to the best fit
+getModelWithOptimalTimeCorrection <- function(split_times)
+{
+        print("In getModelWithOptimalTimeCorrection()")
+        bestTimeCorrection = 0
+        currentTimeCorrection = bestTimeCorrection
+        
+        model <- with(
+                split_times,
+                model_using_splits(distance, time, time_correction = bestTimeCorrection)
+        )
+        # 
+        # print("### Without correction ###")
+        # print(model)
+        
+        minError = 1E6
+        
+        #TODO: Use better algorithm for finding optimal correction
+        while(model$model_fit$RSE < minError){
+                minError = model$model_fit$RSE
+                # print(paste("New minError:", minError))
+                # print(paste("current RSE:", model$model_fit$RSE))
+                currentTimeCorrection = currentTimeCorrection + 0.001
+                # Simple model
+                model <- with(
+                        split_times,
+                        model_using_splits(distance, time, time_correction = currentTimeCorrection)
+                )
+                # print(model$model_fit$RSE)
+                if (model$model_fit$RSE < minError){
+                        bestTimeCorrection = currentTimeCorrection
+                }
+        }
+        
+        model <- with(
+                split_times,
+                model_using_splits(distance, time, time_correction = bestTimeCorrection)
+        )
+        
+        print("### With optimal correction ###")
+        print(paste("Time correction:", bestTimeCorrection))
+        print(model)
+        
+        return(model)
+}
+
 drawSprintFromPhotocells <- function(sprintDynamics, splitTimes, positions, title, plotFittedSpeed = T, 
plotFittedAccel = T, plotFittedForce = T, plotFittedPower = T)
 {
         
diff --git a/r-scripts/tests/shortsLibrary/learningShorts.R b/r-scripts/tests/shortsLibrary/learningShorts.R
index 8bfd5a7d..83a21656 100644
--- a/r-scripts/tests/shortsLibrary/learningShorts.R
+++ b/r-scripts/tests/shortsLibrary/learningShorts.R
@@ -1,19 +1,59 @@
-### ¡¡¡The model needs 3 split times for adjusting without any correction!!!
 
 require(shorts)
+bolt = data.frame(distance = c(10, 20, 30, 40, 50, 60, 70, 80, 90, 100)
+                  , time = c(1.85, 1.02, 0.91, 0.87, 0.85, 0.82, 0.82, 0.82, 0.83, 0.90))
+bolt = data.frame(time = bolt$time, position = bolt$distance, distance = bolt$distance)
 
 split_times3 <- data.frame(
-  distance = c(10, 20, 30),
-  time = c(1.708, 3.059, 4.334)
+    distance = c(10, 20, 30),
+    time = c(1.614, 2.821, 3.966)
 )
 
-# Simple model
-simple_model <- with(
-    split_times3,
-    model_using_splits(distance, time)
-)
-
-print(simple_model)
+# get the model adjusting the time_correction to the best fit
+getModelWithOptimalTimeCorrection <- function(split_times)
+{
+    print("In getModelWithOptimalTimeCorrection()")
+    bestTimeCorrection = 0
+    currentTimeCorrection = bestTimeCorrection
+    
+    model <- with(
+        split_times,
+        model_using_splits(distance, time, time_correction = bestTimeCorrection)
+    )
+    # 
+    # print("### Without correction ###")
+    # print(model)
+    
+    minError = 1E6
+    
+    #TODO: Use better algorithm for finding optimal correction
+    while(model$model_fit$RSE < minError){
+        minError = model$model_fit$RSE
+        # print(paste("New minError:", minError))
+        # print(paste("current RSE:", model$model_fit$RSE))
+        currentTimeCorrection = currentTimeCorrection + 0.001
+        # Simple model
+        model <- with(
+            split_times,
+            model_using_splits(distance, time, time_correction = currentTimeCorrection)
+        )
+        # print(model$model_fit$RSE)
+        if (model$model_fit$RSE < minError){
+            bestTimeCorrection = currentTimeCorrection
+        }
+    }
+    
+    model <- with(
+        split_times,
+        model_using_splits(distance, time, time_correction = bestTimeCorrection)
+    )
+    
+    print("### With optimal correction ###")
+    print(paste("Time correction:", bestTimeCorrection))
+    print(model)
+    
+    return(model)
+}
 
 ### ¡¡¡The model needs 4 split times for adjusting the time!!!
 split_times4 <- data.frame(
@@ -27,6 +67,7 @@ model_with_time_correction_estimation <- with(
     model_using_splits_with_time_correction(distance, time)
 )
 
+print("Model with 4 times")
 print(model_with_time_correction_estimation)
 
 ### ¡¡¡The model needs 5 split times for adjusting the time!!!


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]