From f5c70f6f0133f4d77c6782841e807c679f8504e7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Felix=20Ramnel=C3=B6v?= <felra653@student.liu.se>
Date: Tue, 10 Dec 2024 15:56:03 +0100
Subject: [PATCH] Lab 3: Code cleanup for assignment 2

---
 lab3/assignment2.R | 100 +++++++++++++++++++++++++++++----------------
 1 file changed, 65 insertions(+), 35 deletions(-)

diff --git a/lab3/assignment2.R b/lab3/assignment2.R
index d75dcc0..b0e655d 100644
--- a/lab3/assignment2.R
+++ b/lab3/assignment2.R
@@ -13,7 +13,7 @@ h_time <- 4
 
 a <- 58.4274
 b <- 14.826
-date <- "1960-07-11"
+date <- "1966-12-10"
 times <- c(
   "04:00:00",
   "06:00:00",
@@ -32,25 +32,36 @@ times <- c(
 st <- st[st$date <= date, ] # Filter out posterior dates
 st$time <- strptime(st$time, format = "%H:%M:%S")
 
-dist = seq(0, 300000)
-plot(exp(-abs(dist) ^ 2 / (2 * h_distance ^ 2)),
-     type = 'l',
-     xlab = "Physical distance",
-     ylab = "Kernel")
+gaussian_kernel <- function(x, h) {
+  exp(-(x ^ 2) / (2 * h ^ 2))
+}
+
+dist = seq(0, 300000, length.out = 1000)
+plot(
+  gaussian_kernel(dist, h_distance),
+  type = 'l',
+  xlab = "Physical distance",
+  ylab = "Kernel value",
+  main = "Gaussian distance kernel"
+)
 
-#choosing appropriate smoothing coefficient for days
-dist = seq(0, 100)
-plot(exp(-abs(dist) ^ 2 / (2 * h_date ^ 2)),
-     type = 'l',
-     xlab = "Distance in days",
-     ylab = "Kernel")
+dist = seq(0, 60)
+plot(
+  gaussian_kernel(dist, h_date),
+  type = 'l',
+  xlab = "Distance in days",
+  ylab = "Kernel value",
+  main = "Gaussian date kernel"
+)
 
-#choosing appropriate smoothing coefficient for hours
 dist = seq(0, 18)
-plot(exp(-abs(dist) ^ 2 / (2 * h_time ^ 2)),
-     type = 'l',
-     xlab = "Distance in hours",
-     ylab = "Kernel")
+plot(
+  gaussian_kernel(dist, h_time),
+  type = 'l',
+  xlab = "Distance in hours",
+  ylab = "Kernel value",
+  main = "Gaussian time kernel"
+)
 
 temp_add <- c()
 temp_mult <- c()
@@ -59,6 +70,7 @@ for (time in times) {
   time <- strptime(time, format = "%H:%M:%S")
   
   
+  # Filter out posterior time
   st_temp <- st[st$date < date |
                   (st$date == date &
                      st$time <= time), ]
@@ -66,26 +78,20 @@ for (time in times) {
   
   distance_kernels <- mapply(function(lat, lon) {
     dist <- distHaversine(c(a, b), c(lat, lon))
-    exp(-abs(dist) ^ 2 / (2 * h_distance ^ 2))
-  }, st_temp$latitude, st$longitude)
-  
+    gaussian_kernel(dist, h_distance)
+  }, st_temp$latitude, st_temp$longitude)
   
   
-  date_kernels <- st_temp$date
-  date_kernels <- sapply(date_kernels, function(x_i) {
+  date_kernels <- mapply(function(x_i) {
     dist <- as.numeric(as.Date(date) - as.Date(x_i))
-    exp(-abs(dist) ^ 2 / (2 * h_date ^ 2))
-  })
+    gaussian_kernel(dist, h_date)
+  }, st_temp$date)
   
   
-  time_kernels <- st_temp$time
-  time_kernels <- sapply(time_kernels, function(x_i) {
+  time_kernels <- mapply(function(x_i) {
     dist <- as.numeric(difftime(time, x_i, units = "hours"))
-    exp(-abs(dist) ^ 2 / (2 * h_time ^ 2))
-  })
-  
-  
-  
+    gaussian_kernel(dist, h_time)
+  }, st_temp$time)
   
   
   kernels_add <- distance_kernels + date_kernels + time_kernels
@@ -94,10 +100,34 @@ for (time in times) {
   temp_add <<- c(temp_add,
                  sum(kernels_add * st_temp$air_temperature) / sum(kernels_add))
   temp_mult <<- c(temp_mult,
-                   sum(kernels_mult * st_temp$air_temperature) / sum(kernels_mult))
-  
+                  sum(kernels_mult * st_temp$air_temperature) / sum(kernels_mult))
   
 }
 
-plot(temp_add, type = "o")
-plot(temp_mult, type = "o")
\ No newline at end of file
+plot(
+  temp_add,
+  type = "o",
+  xaxt = "n",
+  xlab = "Time",
+  ylab = "Temperature",
+  main = "Added kernels"
+)
+axis(
+  1,                              
+  at = 1:length(times),         
+  labels = times                  
+)
+
+plot(
+  temp_mult,
+  type = "o",
+  xaxt = "n",
+  xlab = "Time",
+  ylab = "Temperature",
+  main = "Multiplied kernels"
+)
+axis(
+  1,                              
+  at = 1:length(times),         
+  labels = times                  
+)
\ No newline at end of file
-- 
GitLab