[chronojump/shortsLibrary: 38/38] Testing the use of shorts library




commit 954aa8d9f195642a0c1f94102a12e55d090f93fa
Author: Xavier Padullés <testing chronojump org>
Date:   Thu Dec 17 16:51:08 2020 +0100

    Testing the use of shorts library

 r-scripts/sprintPhotocells.R                   | 51 +++++++++++++++++++-------
 r-scripts/tests/shortsLibrary/learningShorts.R | 46 +++++++++++++++++++++++
 2 files changed, 84 insertions(+), 13 deletions(-)
---
diff --git a/r-scripts/sprintPhotocells.R b/r-scripts/sprintPhotocells.R
index be1480bd..26222f30 100644
--- a/r-scripts/sprintPhotocells.R
+++ b/r-scripts/sprintPhotocells.R
@@ -57,12 +57,17 @@ op <- assignOptions(options)
 #Returns the K and Vmax parameters of the sprint using a number of pairs (time, position)
 getSprintFromPhotocell <- function(positions, splitTimes, noise=0)
 {
+        #noise is for testing purpouses.        
         # TODO: If the photocell is not in the 0 meters we must find how long is the time from
         #starting the race to the reaching of the photocell
         # t0 = 0
         # splitTimes = splitTimes + t0
-                                                
-       #noise is for testing purpouses.
+        
+        print("positions:")
+        print(positions)
+        print("splitTimes:")
+        print(splitTimes)
+        
        # Checking that time and positions have the same length
         if(length(splitTimes) != length(positions)){
                 print("Positions and splitTimes have diferent lengths")
@@ -74,23 +79,43 @@ getSprintFromPhotocell <- function(positions, splitTimes, noise=0)
                 print("Not enough data")
                 return()
         }
-        
-        #Asuming that the first time and position are 0s it is not necessary to use the non linear regression
-        #if there's only three positions. Substituting x1 = x(t1), and x2 = x(t2) whe have an exact solution.
-        #2 variables (K and Vmax) and 2 equations.
+
         if (length(positions) == 3){
-                return(getSprintFrom2SplitTimes(positions[2], positions[3], splitTimes[2], splitTimes[3], 
tolerance = 0.0001, initK = 1 ))
+                #Asuming that the first time and position are 0s it is not necessary to use the non linear 
regression
+                #if there's only three positions. Substituting x1 = x(t1), and x2 = x(t2) whe have an exact 
solution.
+                #2 variables (K and Vmax) and 2 equations.
+                model = getSprintFrom2SplitTimes(positions[2], positions[3], splitTimes[2], splitTimes[3], 
tolerance = 0.0001, initK = 1 )
+                K = model$K
+                Vmax = model$Vmax
+        } else if (length(positions) == 4){
+                require(shorts)
+                model <- model_using_splits(positions[2:length(positions)], 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)])
+                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)])
+                K = 1/ model$parameters$TAU
+                Vmax = model$parameters$MSS
         }
         
-        photocell = data.frame(time = splitTimes, position = positions)
-        
+        # photocell = data.frame(time = splitTimes, position = positions)
+        # 
         # Using the model of v = Vmax(1 - exp(-K*t)). If this function are integrated and we calculate the 
integration constant (t=0 -> position = 0)
         # position = Vmax*(time + (1/K)*exp(-K*time)) -Vmax/K
-        pos.model = nls(position ~ Vmax*(time + (1/K)*exp(-K*time)) -Vmax/K, photocell, start = list(K = 
0.81, Vmax = 10), control=nls.control(maxiter=1000, warnOnly=TRUE))
-        K = summary(pos.model)$coeff[1,1]
-        Vmax = summary(pos.model)$coeff[2,1]
+        # pos.model = nls(position ~ Vmax*(time + (1/K)*exp(-K*time)) -Vmax/K, photocell, start = list(K = 
0.81, Vmax = 10), control=nls.control(maxiter=1000, warnOnly=TRUE))
+        # 
+        # K = summary(pos.model)$coeff[1,1]
+        # Vmax = summary(pos.model)$coeff[2,1]
+        # 
+        # summary(pos.model)$coef[1:2,1]
+        
 
-        summary(pos.model)$coef[1:2,1]
         return(list(K = K, Vmax = Vmax))
 }
 
diff --git a/r-scripts/tests/shortsLibrary/learningShorts.R b/r-scripts/tests/shortsLibrary/learningShorts.R
new file mode 100644
index 00000000..8bfd5a7d
--- /dev/null
+++ b/r-scripts/tests/shortsLibrary/learningShorts.R
@@ -0,0 +1,46 @@
+### ¡¡¡The model needs 3 split times for adjusting without any correction!!!
+
+require(shorts)
+
+split_times3 <- data.frame(
+  distance = c(10, 20, 30),
+  time = c(1.708, 3.059, 4.334)
+)
+
+# Simple model
+simple_model <- with(
+    split_times3,
+    model_using_splits(distance, time)
+)
+
+print(simple_model)
+
+### ¡¡¡The model needs 4 split times for adjusting the time!!!
+split_times4 <- data.frame(
+     distance = c(10, 20, 30, 40),
+     time = c(1.893, 3.149, 4.313, 5.444)
+)
+
+# Model with time_correction estimation
+model_with_time_correction_estimation <- with(
+    split_times4,
+    model_using_splits_with_time_correction(distance, time)
+)
+
+print(model_with_time_correction_estimation)
+
+### ¡¡¡The model needs 5 split times for adjusting the time!!!
+split_times5 <- data.frame(
+    distance = c(5, 10, 20, 30, 40),
+    time = c(1.158, 1.893, 3.149, 4.313, 5.444)
+)
+
+# Model with time and distance correction estimation
+model_with_time_distance_correction_estimation <- with(
+    split_times5,
+    model_using_splits_with_corrections(distance, time)
+)
+
+print(model_with_time_distance_correction_estimation)
+
+kimberley_data <- filter(split_times, athlete == "Kimberley")


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