Simple descomposiciĂłn de serie de tiempo de la temperatura media en ValparaĂso entre 1960 y 2021.
# library
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(readxl)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## âś” forcats 1.0.0 âś” readr 2.1.5
## âś” ggplot2 3.5.0 âś” stringr 1.5.1
## âś” lubridate 1.9.3 âś” tibble 3.2.1
## âś” purrr 1.0.2 âś” tidyr 1.3.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## âś– dplyr::filter() masks stats::filter()
## âś– dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readr)
# working data
setwd("C:/RStudio/.Taller 1/")
tm_valpo <- read.csv("Temperatura_media_Valpo.csv")
tm_valpo$fecha <- as.Date(tm_valpo$fecha)
tm_valpo$year <- year(tm_valpo$fecha)
# tabla anual temp media
tabla_anual <- tm_valpo %>%
group_by(year) %>%
summarise(tmedia = mean(Temp, na.rm = TRUE))
# serie de tiempo
ts_anual <- ts(tm_valpo$Temp, start = c(1960, 1), end = c(2021, 12), frequency = 12)
dec_temp <- decompose(ts_anual)
plot(dec_temp)
# anomalias desviacion de media
tabla_anual$Zscore <- scale(tabla_anual$tmedia, scale = T)
ggplot(tabla_anual,
aes(x = year, y = Zscore,
fill = I(ifelse(tabla_anual$Zscore > 0, 'blue', 'brown')),
color = I(ifelse(tabla_anual$Zscore > 0, 'blue', 'brown'))))+
geom_col() + ylab("Anomalia Temperatura (C°)") + xlab("Año") + theme_bw()
## Warning: Use of `tabla_anual$Zscore` is discouraged.
## ℹ Use `Zscore` instead.
## Use of `tabla_anual$Zscore` is discouraged.
## ℹ Use `Zscore` instead.
# remuestreo temporal por año
ggplot(tabla_anual, aes(x = year, y = tmedia)) +
geom_line( ) +
geom_point( ) +
ylab("Temperatura Media (C°)") + xlab("Año")
Algo más complicado, hice una correlación entre los datos de dos estaciones diferentes para luego predecir valores ausentes en una estación por regresión linear. Valores entre 1980 y 2019.
setwd("C:/RStudio/.taller_2/")
###PRECIPITACION BALMACEDA----
balmaceda_PP <- read.csv("Monthly_PP_balmaceda.csv")
balmaceda_PP$date <- as.Date(balmaceda_PP$date)
##SERIE DE TIEMPO
ggplot(balmaceda_PP, aes(x = date, y = value)) +
geom_line( ) + # remuestreo de serie de tiempo
geom_point( ) +
ylab("Precipitacion de Balmaceda (mm)") + xlab("Año")
## Warning: Removed 19 rows containing missing values or values outside the scale range
## (`geom_point()`).
###PRECIPITACION COYHAIQUE----
coyhaique_PP <- read.csv("Monthly_PP_coyhaique.csv")
coyhaique_PP$date <- as.Date(coyhaique_PP$date)
##SERIE DE TIEMPO
ggplot(coyhaique_PP, aes(x = date, y = value)) +
geom_line( ) + # remuestreo de serie de tiempo
geom_point( ) +
ylab("Precipitacion de Coyhaique (mm)") + xlab("Año")
###CORRELACIONES----
# juntamos ambas estaciones en 1 tabla
df <- data.frame(balmaceda = balmaceda_PP$value,
coyhaique = coyhaique_PP$value)
# se grafica la relacion entre ambas estaciones
ggplot(data = df, aes(x = coyhaique, y = balmaceda)) +
geom_point(color = "blue") +
geom_smooth(method = "lm", formula = y~x, se = FALSE, color = "red") +
scale_x_continuous(expand = c(0.005, 0.005)) +
theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
panel.background = element_blank(), axis.line = element_line(colour = "black")) +
ylab("Precipitacion de Balmaceda (mm)") + xlab("Precipitacion de Coyhaique (mm)") + theme_bw()
## Warning: Removed 19 rows containing non-finite outside the scale range
## (`stat_smooth()`).
## Removed 19 rows containing missing values or values outside the scale range
## (`geom_point()`).
# se crea un dataframe limpio sin datos NA
dflimpio <- na.omit(df)
# se reliza la correlacion
cor(x = dflimpio$balmaceda, y = dflimpio$coyhaique, method = "pearson")
## [1] 0.9108149
cor(x = dflimpio$balmaceda, y = dflimpio$coyhaique, method = "kendall")
## [1] 0.7523574
cor(x = dflimpio$balmaceda, y = dflimpio$coyhaique, method = "spearman")
## [1] 0.9161505
# se realiza el test de correlacion el cual resulta en 0.9108149 de conf
cor.test(x = df$coyhaique, y = df$balmaceda,
conf.level = 0.90, method = "pearson")
##
## Pearson's product-moment correlation
##
## data: df$coyhaique and df$balmaceda
## t = 47.269, df = 459, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 90 percent confidence interval:
## 0.8967607 0.9230335
## sample estimates:
## cor
## 0.9108149
cor.test(x = df$coyhaique, y = df$balmaceda,
conf.level = 0.95, method = "pearson")
##
## Pearson's product-moment correlation
##
## data: df$coyhaique and df$balmaceda
## t = 47.269, df = 459, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## 0.8938390 0.9251834
## sample estimates:
## cor
## 0.9108149
cor.test(x = df$coyhaique, y = df$balmaceda,
conf.level = 0.99, method = "pearson")
##
## Pearson's product-moment correlation
##
## data: df$coyhaique and df$balmaceda
## t = 47.269, df = 459, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 99 percent confidence interval:
## 0.8879022 0.9292198
## sample estimates:
## cor
## 0.9108149
cor.test(x = df$coyhaique, y = df$balmaceda,
conf.level = 0.95, method = "kendall")
##
## Kendall's rank correlation tau
##
## data: df$coyhaique and df$balmaceda
## z = 24.119, p-value < 2.2e-16
## alternative hypothesis: true tau is not equal to 0
## sample estimates:
## tau
## 0.7523574
cor.test(x = df$coyhaique, y = df$balmaceda,
conf.level = 0.95, method = "spearman")
## Warning in cor.test.default(x = df$coyhaique, y = df$balmaceda, conf.level =
## 0.95, : Cannot compute exact p-value with ties
##
## Spearman's rank correlation rho
##
## data: df$coyhaique and df$balmaceda
## S = 1369147, p-value < 2.2e-16
## alternative hypothesis: true rho is not equal to 0
## sample estimates:
## rho
## 0.9161505
###REGRESION LINEAL----
lm(balmaceda ~ coyhaique, data = df)
##
## Call:
## lm(formula = balmaceda ~ coyhaique, data = df)
##
## Coefficients:
## (Intercept) coyhaique
## -3.5004 0.5745
modelo <- lm(balmaceda ~ coyhaique, data = df)
summary(modelo)
##
## Call:
## lm(formula = balmaceda ~ coyhaique, data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -58.603 -7.532 -1.893 5.684 85.959
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -3.50037 1.20323 -2.909 0.0038 **
## coyhaique 0.57452 0.01215 47.269 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 14.53 on 459 degrees of freedom
## (19 observations deleted due to missingness)
## Multiple R-squared: 0.8296, Adjusted R-squared: 0.8292
## F-statistic: 2234 on 1 and 459 DF, p-value: < 2.2e-16
# se recopilan los datos de coyhaique que se van a ocupar para predecir el modelo linear
new_data <- data.frame(coyhaique = c(53, 72.4, 130.3, 50.9, 112.2, 37.4, 19.9, 60.6, 87.1,
123.5, 97.8, 56.4, 136.7, 163.0, 99.6, 218.5, 37.5, 251.8, 22.1))
predict(modelo, newdata = new_data, method = "pearson")
## 1 2 3 4 5 6 7
## 26.949407 38.095174 71.360118 25.742907 60.961232 17.986832 7.932660
## 8 9 10 11 12 13 14
## 31.315790 46.540678 67.453354 52.688086 28.902789 75.037072 90.147055
## 15 16 17 18 19
## 53.722229 122.033141 18.044284 141.164793 9.196613