-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractical Two.qmd
More file actions
78 lines (65 loc) · 1.95 KB
/
Practical Two.qmd
File metadata and controls
78 lines (65 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
---
title: "Practical Two"
author: "Katinka Wilkinson"
date: "07/02/2025"
format: html
---
## Question 1
## Part 1: Generating Simulated Data
```{r}
library(ggplot2)
set.seed(1)
x <- 1:100
y <- sin(x/10) + rnorm(100, 0, 0.2^2)
head(y)
```
```{r}
ggplot(mapping = aes(x = x, y = y)) +
geom_point() +
labs(title = "Raw data")
```
## Part 2: Implementing the LOWESS Algorithm
```{r}
customLowess <- function(x, y, f){
# A function that implements the LOWESS algorithm and returns smoothed values.
#
# Args:
# x (numeric vector) - the x values in the data set
# y (numeric vector) - the y values in the data set
# f (numeric) - the spac of the smoothing where 0<f<1
#
# Return:
# y (numeric vector) - the smoothed y values
# Span
k <- floor(f*length(x)) # computing the number of neighbouring points used for smoothing
smoothened_ys <- numeric(length(x))
for (i in 1:length(x)) {
# Computing weights
current_x <- x[i]
distances <- abs(x - current_x)
all_values <- cbind(x, distances, y)
sorted_values <- all_values[order(all_values[,2]),]
k_neighbours <- sorted_values[1:k,]
d_max <- k_neighbours[k,2]
weights <- (1-(k_neighbours[,2]/d_max)^3)^3
# Weighted Regression
W = diag(weights, ncol=k)
x_neigh <- cbind(1,k_neighbours[,1]) # here, x is a design matrix, meaning that the first column is simply a coulmn on 1s
y_neigh <- k_neighbours[,3]
betas <- solve(t(x_neigh) %*% W %*% x_neigh) %*% t(x_neigh) %*% W %*% y_neigh
smoothened_ys[i] = betas[1,1] + betas[2,1]*current_x
}
return(smoothened_ys)
}
smooth_ys <- customLowess(x,y,0.2)
ggplot(mapping = aes(x = x, y = smooth_ys)) +
geom_point() +
labs(title = "Data smoothened using custom LOWESS function", x = "x", y = "y")
```
## Question 3
```{r}
lowess_output <- lowess(x, y, 0.2)
ggplot(mapping = aes(x = x, y = lowess_output$y)) +
geom_point() +
labs(title = "Data smoothened using built-in lowess function",x = "x", y = "y")
```