Skip to content

Instantly share code, notes, and snippets.

View juanchiem's full-sized avatar
😳

Juan Edwards Molina juanchiem

😳
View GitHub Profile
@juanchiem
juanchiem / outlierRemoval.R
Created February 27, 2017 12:20 — forked from smithdanielle/outlierRemoval.R
Outlier removal in R with MAD criterion: equations taken from [Leys et al (2013)](http://www.sciencedirect.com/science/article/pii/S0022103113000668).
require(plyr)
require(dplyr)
data.outliersRemoved<-ddply(data, .(withinIV1, withinIV2), function(d){
limits.outliers = median(d$DV) + 2.5*c(-1, 1)*mad(d$DV))
d$DV[which(((d$DV - limits.outliers[1])*(limits.outliers[2] - d$DV)) <= 0)]<-NA
return(d)
})
@juanchiem
juanchiem / script_exemplo_metanalise.R
Created May 12, 2017 19:17 — forked from joaovissoci/script_exemplo_metanalise.R
example of R script for metanalysis in R
#######################################################################################
#example_metanalysis.R is licensed under a Creative Commons Attribution - Non commercial 3.0 Unported License. see full license at the end of this file.
#######################################################################################
#this script follows a combination of the guidelines proposed by Hadley Wickham http://goo.gl/c04kq as well as using the formatR package http://goo.gl/ri6ky
#if this is the first time you are conducting an analysis using this protocol, please watch http://goo.gl/DajIN while following step by step
#link to manuscript
#####################################################################################
#SETTING ENVIRONMENT
library(tidyverse)
# devtools::install_github('rensa/ggflags')
library(ggflags) # https://github.com/rensa/ggflags
library(ggrepel)
library(ggthemes)
library(gganimate)
# Hecho por Rafa @GonzalezGouveia
# Con gusto para #DatosDeMiercoles, propuesto por @R4DS_es
library(ggplot2)
ggplot(data = data.frame(x = c(-4, 6)), aes(x)) +
stat_function(fun = dnorm, n = 101, args = list(mean = 0, sd = 1), geom = "area", fill = "#F5BB87", alpha = 0.9) +
stat_function(fun = dnorm, n = 101, args = list(mean = 2.7, sd = 1), geom = "area", fill = "#BBD4EB", alpha = 0.9) +
coord_fixed(xlim = c(-5, 7), ratio = 8, clip = "off") +
annotate("segment", x = -3.9, y = 0, xend = 6, yend = 0, color = "grey60", size = 1.3) +
annotate("segment", x = -3.5, y = -0.04, xend = -3.5, yend = 0.42, color = "grey60", size = 1.3) +
annotate("text", x = 0, y = 0.18, label = "what people\nactually do\nin R", family = "Arial Bold", color = "white", size = 5.2) +
annotate("text", x = 2.75, y = 0.18, label = "what people\nassume others\ndo in R", family = "Arial Bold", color = "white", size = 5.2) +
library(tidyverse)
library(zoo)
df <- data.frame(
date = seq(as.Date("2002-01-01"), by="1 day", length.out=50) ,
var = 100 + c(0, cumsum(runif(49, -20, 20))))
df %>%
mutate(ma5=rollmean(var,5,align='right',fill=NA))%>%
mutate(sum5=rollsum(var,2,align='right',fill=NA))%>%
pacman::p_load(tidyverse, lme4, sjPlot, car)
dat <- read_csv("https://github.com/juanchiem/agro_data/raw/master/yield_wheat19.csv")
#Acondicionamiento del dataset
red_t=c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L)
id_trt <- tibble::tribble(
~trt, ~trat, ~kg_trigo,
1, "1.Check", 0,
2, "2.MD_z32", 172,
library(tidyverse)
library(googlesheets4)
gapminder <- gs4_get("https://docs.google.com/spreadsheets/d/1U6Cf_qEOhiR9AZqTqS3mbMF3zt2db48ZP5v3rkrAEJY/edit#gid=780868077")
# gapminder es una planilla de google sheets que tiene 5 hojas (una por continente)
# Cuando cada hoja tiene toda la información que necesitamos (como este caso)
gapminder_data <- gapminder %>% sheet_names() %>%
map_df(~ read_sheet(gapminder, sheet = .)) %>%
library(ggpubr)
x <- 1:100
y <- (x + x^2 + x^3) + rnorm(length(x), mean = 0, sd = mean(x^3) / 4)
my.data <- data.frame(x, y, group = c("A", "B"),
y2 = y * c(0.5,2), block = c("a", "a", "b", "b"))
# Fit polynomial regression line and add labels
formula <- y ~ poly(x, 3, raw = TRUE)
p <- ggplot(my.data, aes(x, y2, color = group)) +
geom_point() +
# library("googledrive")
library("googlesheets4")
library("tidyverse")
library("ggpubr")
# drive_auth(use_oob = TRUE, cache = TRUE)
#
mysheets <- googledrive::drive_find(type = "spreadsheet")
mysheets
# reuse token to Sheet Auth.
# https://itsalocke.com/blog/understanding-rolling-calculations-in-r/
# https://inta.gob.ar/sites/default/files/inta_impacto_de_los_factores_ambientales_en_la_definicion_de_los_rendimientos_de_los_cultivos_resultados_de_la_campana_2013_de_trigo.pdf
# http://rafaela.inta.gov.ar/info/miscelaneas/101/trigo2004_n1.pdf
pacman::p_load(tidyverse, lubridate, zoo)
stations <- tibble::tribble(
~station, ~lat, ~lon,
"Balcarce INTA", -37.75, -58.3,
"Mar del Plata AERO", -37.93, -57.58,