---
title: "Location Based Services"
author: "Erik Neller"
date: "`r Sys.Date()`"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
rm(list=ls())
```
# Data Info
- GPST: Timestamp
- latitude/longitude/height: Position estimates.
- Q: Quality indicator (e.g., 5 = single-point positioning).
- ns: Number of satellites used
- sdn/sde/sdu: Standard deviations (North, East, Up)
- sdne/sdeu/sdun: Combined standard deviations



```{r echo=FALSE}
library(DataExplorer)
library(sf)
library(lubridate)
earthrad <- 6378137
d <- read.csv('rec.csv',header = FALSE, sep='',skip=15)

doy <- yday(d$V1)
res1 <- ymd_hms(paste(d$V1,d$V2, sep=' '))
hour <- hour(res1)
min <- minute(res1)
sec <- second(res1)

timer <- doy + hour/24 + min/(24*60) + sec/(24*3600)
# calculate the error of lat lon elevation
latitude <- (d$V3 + 12.843697222222223)*earthrad*pi/180
longitude <- (d$V4 - 131.13274444444443)*earthrad*pi/180
height <- d$V5 - 125.1
df <- as.data.frame(cbind(longitude, latitude, height))
```
# Error Plot
```{r echo=FALSE}
plot(timer, latitude, type='l', col = 'red', 
     ylim = c(min(latitude, longitude,height),
              max(latitude, longitude,height)),
     xlab='time [day in 2015]',ylab='errors [m]')
lines(timer, longitude, col= 'dark green')
lines(timer, height, col= 'blue')
legend('topright', legend=c('latitude', 'longitude','height'),
       col=c('red','dark green', 'blue'), lty=1:1, cex=0.8,
       box.lty=0)

```
# Error Summary
```{r}
summary(latitude)
summary(longitude)
summary(height)
```

# Tests
```{r}
shapiro.test(latitude)
ks.test(latitude, 'pnorm')

shapiro.test(longitude)
ks.test(longitude, 'pnorm')

shapiro.test(height)
ks.test(height, 'pnorm')

ks.test(latitude, 'pnorm')
```
Both SW and KS Test suggest that the errors do not follow a normal distribution which is supported by visual inspection:

```{r visualerr}
library(ggplot2)
  ggplot(df, aes(x = latitude)) +
  geom_histogram(bins = 30, fill = "blue", alpha = 0.7) +
  ggtitle("Distribution of Latitude Error") +
  xlab("Error (m)") +
  ylab("Frequency")

ggplot(df, aes(x = longitude)) +
  geom_histogram(bins = 30, fill = "green", alpha = 0.7) +
  ggtitle("Distribution of Longitude Error") +
  xlab("Error (m)") +
  ylab("Frequency")

ggplot(df, aes(x = height)) +
  geom_histogram(bins = 30, fill = "red", alpha = 0.7) +
  ggtitle("Distribution of Height Error") +
  xlab("Error (m)") +
  ylab("Frequency")
```
```{r}
var.test(latitude, longitude)
cor.test(latitude, longitude)

var.test(height, longitude)
cor.test(height, longitude)

var.test(latitude, height)
cor.test(latitude, height)





```
Variances of the samples are not equal and not similar.
However, the correlation of latitude and height is highly correlated, suggesting a linear relationship.
# Grouping by numsatellites
```{r numsatellites}
d_split <- split(d,d$V7)
summary(d_split)
```