Skip to content

Instantly share code, notes, and snippets.

View RamiKrispin's full-sized avatar
👻
Focusing

Rami Krispin RamiKrispin

👻
Focusing
View GitHub Profile
@RamiKrispin
RamiKrispin / Rstudio_code_snippet.txt
Created November 19, 2021 05:34
My RStudio Code Snippets
snippet dd
devtools::document()
snippet m
`%>%` <- magrittr::`%>%`
snippet l
lubridate::
snippet >
%>%
snippet p
plotly::
@RamiKrispin
RamiKrispin / covid19sf_demo.R
Created July 3, 2021 14:01
Distribution of San Francisco Vaccinated Population by Age Group
data("covid19sf_vaccine_demo")
d1 <- covid19sf_vaccine_demo
head(d1)
`%>%` <- magrittr::`%>%`
d1a <- covid19sf_vaccine_demo %>%
dplyr::filter(administering_provider_type == "All Providers",
demographic_group == "Age Bracket",
age_group == "All") %>%
dplyr::mutate(only_1st = total_1st_doses - total_2nd_doses,
not_vaccinated = subgroup_population - total_series_completed - only_1st,
@RamiKrispin
RamiKrispin / remove_dot.R
Created May 27, 2021 13:29
Parsing the text follow the dot
x <- "Plan_Fc.Plan_Fc_Doppler_US"
s <- unlist(regexec(pattern = "\\.", text = x)) + 1
e <- nchar(x)
substr(x = x, start = s, stop = e)
@RamiKrispin
RamiKrispin / multiple_forecast_plot.R
Created May 19, 2021 21:16
Example of plotting multiple forecasts in a single plot
library(cdcfluview)
national_ili <- ilinet("national", years = c(1997:2017))
mydata.ts <- ts(data = national_ili$total_patients, end = c(2018,39), frequency= 52)
plot(mydata.ts,type="o",col="blue")
plot(forecast(mydata.ts, h = 65))
library(coronavirus)
library(dplyr)
library(forecast)
@RamiKrispin
RamiKrispin / two_yaxis.R
Created May 17, 2021 20:03
example for two y axis
library(coronavirus)
library(dplyr)
raw <- refresh_coronavirus_jhu()
head(raw)
df <- raw %>%
filter(location == "Israel",
data_type == "cases_new") %>%
select(date, new_cases = value) %>%
arrange(date)
head(df)
@RamiKrispin
RamiKrispin / mclapply_sim.R
Created May 16, 2021 06:03
Example for using mclapply for running simulation in parallel
# Train ARIMA model
md <- arima(x = AirPassengers, order = c(2, 1, 2), seasonal = list(order = c(0, 1, 0)))
# Set number of simulations
n <- 200
#Run simulation
sim <- parallel::mclapply(1:n,
mc.cores = 12,
function(i){
fc_sim <- simulate(md, 24)
@RamiKrispin
RamiKrispin / week_index.R
Created May 15, 2021 21:09
Example for creating weekly index
df <- data.frame(date = seq.Date(from = as.Date("2020-01-01"),
length.out = 100,
by = "week")) %>%
dplyr::mutate(week_index = 1 + (as.numeric(date) - as.numeric(min(date)))/7)
head(df)
@RamiKrispin
RamiKrispin / covid19_death_dist.R
Last active April 29, 2021 11:14
Covid19 Death Dist Plot
library(coronavirus)
library(dplyr)
library(plotly)
df <- refresh_coronavirus_jhu()
head(df)
df1 <- df %>%
filter(data_type == "deaths_new") %>%
group_by(location) %>%
summarise(total = sum(value),
@RamiKrispin
RamiKrispin / new_england_gas.R
Last active April 3, 2021 12:28
New England Annual Gas Consumption by State
library(USgas)
data("us_total")
# Set labels
ne <- c("Connecticut", "Maine", "Massachusetts",
"New Hampshire", "Rhode Island", "Vermont")
# Filter the data
ne_gas <- us_total[which(us_total$state %in% ne),]
# Transform to wide format
@RamiKrispin
RamiKrispin / us_elec_dist.R
Created April 2, 2021 19:00
Distribution of energy sources used to generate electricity in the US during 2020
# Based on this example - https://jkunst.com/highcharter/articles/fontawesome.html
# Data from the USgrid package - https://github.com/RamiKrispin/USgrid
# Icons from - https://fontawesome.com/icons?d=gallery&p=2
library(fontawesome)
library(highcharter)
library(stringr)
library(dplyr) # to wokr with list columns
library(purrr) # to wokr with list columns