library("Rbitcoin")
trades <- market.api.process('kraken',c('BTC','EUR'),'trades')
Rbitcoin.plot(trades)
bitcoin <- Quandl("BITCOIN/B2CUSD")
head(bitcoin)
tail(bitcoin)
daily_ts <- xts(bitcoin$Close,order.by=bitcoin$Date)
daily_ts
head(bitcoin)
head(daily_ts)
daily_ts
head(daily_ts)
dygraph(daily_ts,ylab="US Dollars",
main="bitcoin/USD Close Price") %>%
dySeries("V1",label="Avg_Buy") %>%
dyRangeSelector(dateWindow = c("2016-10-01","2017-09-30"))
daily_ts
tail(daily_ts)
names(daily_ts)
names(daily_ts) <- "V1"
head(daily_ts)
tail(daily_ts)
# Plot with htmlwidget dygraph
dygraph(daily_ts,ylab="US Dollars",
main="bitcoin/USD Close Price") %>%
dySeries("V1",label="Avg_Buy") %>%
dyRangeSelector(dateWindow = c("2016-10-01","2017-09-30"))
data <- select(raw,-unixtime)
rm(raw)
data <- mutate(data,value = price * amount)
by_date <- group_by(data,date)
daily <- summarise(by_date,count = n(),
m_price <-  mean(price, na.rm = TRUE),
m_amount <- mean(amount, na.rm = TRUE),
m_value <-  mean(value, na.rm = TRUE))
names(daily) <- c("date","count","m_value","m_price","m_amount")
head(daily,2)
tail(daily,2)
# Make the m_value variable into a time series object
daily_ts <- xts(daily$m_value,order.by=daily$date)
# Plot with htmlwidget dygraph
dygraph(daily_ts,ylab="US Dollars",
main="Average Value of bitstampUSD Buys") %>%
dySeries("V1",label="Avg_Buy") %>%
dyRangeSelector(dateWindow = c("2011-09-13","2015-11-02"))
daily_ts
daily
daily_ts
head(daily_ts)
tail(daily_ts)
bitcoin <- Quandl("BITCOIN/B2CUSD")
head(bitcoin)
tail(bitcoin)
# Make the m_value variable into a time series object
daily_ts <- xts(bitcoin$Close,order.by=bitcoin$Date)
head(daily_ts)
tail(daily_ts)
dygraph(daily_ts,ylab="US Dollars",
main="bitcoin/USD Close Price") %>%
dySeries("V1",label="Avg_Buy") %>%
dyRangeSelector(dateWindow = c("2016-10-02","2017-09-20"))
dygraph(daily_ts,ylab="US Dollars",
main="bitcoin/USD Close Price") %>%
dySeries("V1",label="Avg_Buy") %>%
dyRangeSelector(dateWindow = c("2017-01-01","2017-09-30"))
daily_ts <- xts(bitcoin,order.by=bitcoin$Date)
head(daily_ts)
tail(daily_ts)
names(daily_ts) <- "V1"
# Plot with htmlwidget dygraph
dygraph(daily_ts,ylab="US Dollars",
main="bitcoin/USD Close Price") %>%
dySeries("V1",label="Avg_Buy") %>%
dyRangeSelector(dateWindow = c("2017-01-01","2017-09-30"))
daily_ts <- xts(bitcoin[,c("Open","Close")],order.by=bitcoin$Date)
head(daily_ts)
tail(daily_ts)
dygraph(daily_ts,ylab="US Dollars",
main="bitcoin/USD Close Price") %>%
dySeries("Open",label="Avg_Buy") %>%
dyRangeSelector(dateWindow = c("2017-01-01","2017-09-30"))
dygraph(daily_ts,ylab="US Dollars",
main="bitcoin/USD Close Price") %>%
dySeries("Open",label="Avg_Buy") %>%
dyRangeSelector(dateWindow = c("2017-01-01","2017-09-30"))
tail(bitcoin)
daily_ts <- xts(bitcoin[,c("Open","Close","Weighted Price")],order.by=bitcoin$Date)
head(daily_ts)
tail(daily_ts)
daily_ts$Weighted Price
daily_ts$Weighted_Price
daily_ts$Weighted.Price
daily_ts[,"Weighted.Price"]
daily_ts[,"Weighted Price"]
dygraph(daily_ts,ylab="US Dollars",
main="bitcoin/USD Close Price") %>%
dySeries("Weighted Price",label="Avg_Buy") %>%
dyRangeSelector(dateWindow = c("2017-01-01","2017-09-30"))
daily_ts <- daily_ts[,"Weighted Price"]
tail(daily_ts)
dygraph(daily_ts,ylab="US Dollars",
main="bitcoin/USD Close Price") %>%
dySeries("Weighted Price",label="Avg_Buy") %>%
dyRangeSelector(dateWindow = c("2017-01-01","2017-09-30"))
remove_outliers <- function(x, na.rm = TRUE, ...) {
qnt <- quantile(x, probs=c(.01, .99), na.rm = na.rm, ...)
H <- 1.5 * IQR(x, na.rm = na.rm)
y <- x
y[x < (qnt[1] - H)] <- NA
y[x > (qnt[2] + H)] <- NA
y
}
set.seed(1)
x <- rnorm(100)
x <- c(-10, -5, x, 5, 10)
y <- remove_outliers(x)
## png()
par(mfrow = c(1, 2))
boxplot(x)
boxplot(y)
boxplot(daily_ts[,"Weighted Price"])
daily_ts[,"Weighted Price"]
as.data.frame(daily_ts[,"Weighted Price"])
boxplot(as.data.frame(daily_ts[,"Weighted Price"]))
remove_outliers <- function(x, na.rm = TRUE, ...) {
qnt <- quantile(x, probs=c(.00, .99), na.rm = na.rm, ...)
H <- 1.5 * IQR(x, na.rm = na.rm)
y <- x
y[x < (qnt[1] - H)] <- NA
y[x > (qnt[2] + H)] <- NA
y
}
daily_ts2 <- remove_outliers(daily_ts1)
boxplot(as.data.frame(daily_ts2[,"Weighted Price"]))
daily_ts2 <- remove_outliers(daily_ts)
boxplot(as.data.frame(daily_ts2[,"Weighted Price"]))
remove_outliers <- function(x, na.rm = TRUE, ...) {
qnt <- quantile(x, probs=c(.00, .999), na.rm = na.rm, ...)
H <- 1.5 * IQR(x, na.rm = na.rm)
y <- x
y[x < (qnt[1] - H)] <- NA
y[x > (qnt[2] + H)] <- NA
y
}
daily_ts2 <- remove_outliers(daily_ts)
boxplot(as.data.frame(daily_ts2[,"Weighted Price"]))
remove_outliers <- function(x, na.rm = TRUE, ...) {
qnt <- quantile(x, probs=c(.00, .99), na.rm = na.rm, ...)
H <- 1.5 * IQR(x, na.rm = na.rm)
y <- x
y[x < (qnt[1] - H)] <- NA
y[x > (qnt[2] + H)] <- NA
y
}
daily_ts2 <- remove_outliers(daily_ts)
boxplot(as.data.frame(daily_ts2[,"Weighted Price"]))
par(mfrow = c(1, 2))
boxplot(as.data.frame(daily_ts[,"Weighted Price"]))
boxplot(as.data.frame(daily_ts2[,"Weighted Price"]))
# Plot with htmlwidget dygraph
dygraph(daily_ts2,ylab="US Dollars",
main="bitcoin/USD Weighted Price") %>%
dySeries("Weighted Price",label="Weighted Price") %>%
dyRangeSelector(dateWindow = c("2017-01-01","2017-09-30"))
bitcoin <- Quandl("BITCOIN/B2CUSD")
head(bitcoin)
tail(bitcoin)
bitcoin2 <- Quandl("BITSTAMP/USD")
head(bitcoin2)
tail(bitcoin2)
daily_ts <- xts(bitcoin2[,c("Last","Volume")],order.by=bitcoin$Date)
head(daily_ts)
tail(daily_ts)
daily_ts <- xts(bitcoin2[,c("Last","Volume")],order.by=bitcoin2$Date)
head(daily_ts)
tail(daily_ts)
daily_ts_last <- daily_ts[,"Last"]
# Plot with htmlwidget dygraph
dygraph(daily_ts_last,ylab="US Dollars",
main="bitcoin/USD Close Price") %>%
dySeries("Last",label="Last Price") %>%
dyRangeSelector(dateWindow = c("2017-01-01","2017-09-30"))
daily_ts_volume <- daily_ts[,"Volume"]
# Plot with htmlwidget dygraph
dygraph(daily_ts_volume,ylab="US Dollars",
main="bitcoin Volume") %>%
dySeries("Volume",label="Volume") %>%
dyRangeSelector(dateWindow = c("2017-01-01","2017-09-30"))
rmarkdown::shiny_prerendered_clean('sweave.Rnw')
knit_with_parameters('D:/PROJECT/R/Slide_Beamer.Rmd', encoding = 'UTF-8')
install.packages("beamer")
install.packages("beamer")
unlink('Slide_Beamer2_cache', recursive = TRUE)
knit_with_parameters('D:/PROJECT/R/Slide_Beamer2.Rmd')
library("rmarkdown", lib.loc="D:/Program Files/R/R-3.4.1/library")
install.packages("rmarkdown")
install.packages('prettydoc')
data.frame(x = c(1,2,3,4), y=c(1,2,3,4))
knitr::kable(data.frame(
Товар = c(1,2,3,4),
y = c(1,2,3,4)))
knitr::kable(data.frame(
x = c(1,2,3,4),
y = c(1,2,3,4)))
data.frame(
x = c(1,2,3,4),
y = c(1,2,3,4))
setwd("D:/PROJECT/R/")
dir()
setwd("D:/PROJECT/R/yctrl")
dir()
setwd("D:/PROJECT/R/yctrl/yctrl_tenders")
dir()
setwd("D:/PROJECT/R/yctrl/yctrl_tenders")
dir()
setwd("D:/PROJECT/R/yctrl/yctrl_tenders/version1.0/img")
dir()
setwd("D:/PROJECT/R/yctrl/yctrl_tenders/version1.0/img")
dir()
install.packages("xlsx")
# devtools::install_github("maxconway/gsheet")
# install.packages('animation')
# install.packages('ggrepel')
# install.packages("xlsx")
library(dplyr)
library(ggplot2)
library("gsheet")
library(reshape2)
library("ggthemes")
library(clusterSim)
library(scales)
library(ggrepel)
library(lubridate)
library(zoo)
library(xts)
###----
# Set the Date
###----
end_date <- as.Date("2018-04-01")
end_date2 <- as.Date("2018-01-01")
setwd("D:/PROJECT/R/data_exploratory_dar")
list.files()
ozone <- read_csv("data/hourly_44201_2015.csv", col_types = "ccccinnccccccncnnccccccc")
head(ozone)
ozone <- read_csv("data/hourly_44201_2015.csv", col_types = "ccccinnccccccncnnccccccc")
ozone <- read.csv("data/hourly_44201_2015.csv", col_types = "ccccinnccccccncnnccccccc")
#####
library("dplyr")
library("ggplot2")
library(readr)
ozone <- read_csv("data/hourly_44201_2015.csv", col_types = "ccccinnccccccncnnccccccc")
head(ozone)
glimpse(ozone)
View(ozone)
View(ozone)
nrow(ozone)
ncol(ozone)
str(ozone)
describe(ozone)
summary(ozone)
dim(ozone)
head(ozone[, c(6:7, 10)], 10)
tail(ozone[, c(6:7, 10)])
ozone[10000:10005, c(6:7, 10)]
table(ozone$Time.Local)
table(ozone$Time.Local)
2<3
filter(ozone, Time.Local == "07:00") %>%
select(State.Name, County.Name, Date.Local,
Time.Local, Sample.Measurement)
filter(ozone, State.Name == "Alabama"
& County.Name == "Baldwin"
& Date.Local == "2015-03-01")
names(ozone)
filter(ozone, Time_Local == "07:00")
filter(ozone, Latitude == 33.54528)
filter(ozone, Latitude == "33.54528")
# Set working Directory
setwd("D:/PROJECT/R/DSFC/lesson_10_20") #tut zadaemo shlyah do robochoi papky
dir()
# Install packages (if needed):
# install.packages("psych")
# install.packages("dplyr")
# install.packages("ggplot2", "GGally")
# install.packages("reshape")
# install.packages("nycflights13")
# Activate libraries:
library("tidyverse")
library("psych")
library("dplyr")
library("ggplot2")
library("GGally")
library("reshape")
library("nycflights13")
###----
# Quick analysis of our dataset:
###----
?flights
?diamonds
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))
# Histogram: continuous variable plotting:
ggplot(data = diamonds) +
geom_histogram(mapping = aes(x = carat), binwidth = 0.5)
# Histogram: just for smaller diamonds
smaller <- diamonds %>%
filter(carat < 3)
smaller <- diamonds %>%
filter(carat < 3)
ggplot(data = smaller, mapping = aes(x = carat)) +
geom_histogram(binwidth = 0.1)
# overlay multiple histograms in the same plot
ggplot(data = smaller, mapping = aes(x = carat, colour = cut)) +
geom_freqpoly(binwidth = 0.1)
diamonds2 <- diamonds %>%
mutate(y = ifelse(y < 3 | y > 20, NA, y))
diamonds2
ggplot(data = diamonds2, mapping = aes(x = x, y = y)) +
geom_point()
ggplot(data = diamonds, mapping = aes(x = x, y = y)) +
geom_point()
ggplot(data = diamonds2, mapping = aes(x = x, y = y)) +
geom_point()
sum(diamonds2$y)
sum(diamonds2$y, na.rm = TRUE)
ggplot(data = diamonds, mapping = aes(x = price)) +
geom_freqpoly(mapping = aes(colour = cut), binwidth = 500)
ggplot(diamonds) +
geom_bar(mapping = aes(x = cut))
ggplot(data = diamonds, mapping = aes(x = price, y = ..density..)) +
geom_freqpoly(mapping = aes(colour = cut), binwidth = 500)
ggplot(data = diamonds, mapping = aes(x = cut, y = price)) +
geom_boxplot()
ggplot(data = diamonds, mapping = aes(x = cut, y = carat)) +
geom_boxplot()
# how highway mileage varies across classes:
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot()
ggplot(data = mpg) +
geom_boxplot(mapping = aes(x = reorder(class, hwy, FUN = median), y = hwy))
ggplot(data = mpg) +
geom_boxplot(mapping = aes(x = reorder(class, hwy, FUN = median), y = hwy)) +
coord_flip()
ggplot(data = diamonds) +
geom_point(mapping = aes(x = carat, y = price))
ggplot(data = diamonds) +
geom_point(mapping = aes(x = carat, y = price), alpha = 1 / 100)
ggplot(data = smaller) +
geom_bin2d(mapping = aes(x = carat, y = price))
# install.packages("hexbin")
ggplot(data = smaller) +
geom_hex(mapping = aes(x = carat, y = price))
# you could bin carat and then for each group, display a boxplot:
ggplot(data = smaller, mapping = aes(x = carat, y = price)) +
geom_boxplot(mapping = aes(group = cut_width(carat, 0.1)))
ggplot(data = smaller, mapping = aes(x = carat, y = price)) +
geom_boxplot(mapping = aes(group = cut_width(carat, 0.5)))
# you could bin carat and then for each group, display a boxplot:
ggplot(data = smaller, mapping = aes(x = carat, y = price)) +
geom_boxplot(mapping = aes(group = cut_width(carat, 0.1)))
ggplot(data = smaller, mapping = aes(x = carat, y = price)) +
geom_boxplot(mapping = aes(group = cut_number(carat, 20)))
ggplot(data = faithful, mapping = aes(x = eruptions)) +
geom_freqpoly(binwidth = 0.25)
#Short version
ggplot(faithful, aes(eruptions)) +
geom_freqpoly(binwidth = 0.25)
# Data Visualization: ggplot2. Distribution plots
# Script for lesson #5
# Data Science Financial Club KNEU
# Lecturer: Roman Kornyliuk
# Date: 2017-06-27
# Source 1: http://www.cookbook-r.com/Graphs/Plotting_distributions_(ggplot2)/
# install.packages("tidyverse")
# install.packages("ggplot2")
# install.packages('mvtnorm')
library(tidyverse)
library(ggplot2)
library(mvtnorm)
# The seed number you choose is the starting point
# used in the generation of a sequence of random numbers
set.seed(1234)
# create data frame
dat <- data.frame(sec = factor(rep(c("A","B"), each=200)),
rating = c(rnorm(200), rnorm(200, mean=0.8) ))
# View first & last few rows
head(dat,15)
tail(dat)
dim(dat)
###----
# Histograms
###----
## Basic histogram from the vector "rating". Each bin is .5 wide.
## These both result in the same output:
ggplot(dat, aes(x=rating)) +
geom_histogram()
ggplot(dat, aes(x=rating)) +
geom_histogram(binwidth=.5)
# Draw with black outline, white fill
ggplot(dat, aes(x=rating)) +
geom_histogram(binwidth=.5, colour="black", fill="white")
dat <- data.frame(sec = factor(rep(c("A","B"), each=200)),
rating = c(rnorm(200), rnorm(200, mean=0.8) ))
# View first & last few rows
head(dat,15)
tail(dat)
ggplot(dat, aes(x=rating)) +
geom_histogram(binwidth=.5)
# Draw with black outline, white fill
ggplot(dat, aes(x=rating)) +
geom_histogram(binwidth=.5, colour="black", fill="white")
# Draw with black outline, white fill
ggplot(dat, aes(x=rating)) +
geom_histogram(binwidth=.5, colour="red", fill="blue")
# Draw with black outline, white fill
ggplot(dat, aes(x=rating)) +
geom_histogram(binwidth=.5, colour="red", fill="grey")
# Density curve
ggplot(dat, aes(x=rating)) +
geom_density()
median(dat$rating)
mean(dat$rating)
# Interleaved histograms
ggplot(dat, aes(x=rating, fill=sec)) +
geom_histogram(binwidth=.5, position="dodge")
# Overlaid histograms
ggplot(dat, aes(x=rating, fill=sec)) +
geom_histogram(binwidth=.5, alpha=0.5, position="identity")
# Density plots
ggplot(dat, aes(x=rating, colour=sec)) +
geom_density()
# Density plots with semi-transparent fill
ggplot(dat, aes(x=rating, fill=sec)) +
geom_density(alpha=.3)
# Find the mean of each group
library(plyr)
cdat <- ddply(dat, "sec", summarise, rating.mean=mean(rating))
cdat
# Overlaid histograms with means
ggplot(dat, aes(x=rating, fill=sec)) +
geom_histogram(binwidth=.5, alpha=.5, position="identity") +
geom_vline(data=cdat, aes(xintercept=rating.mean,  colour=sec),
linetype="dashed", size=1)
# Density plots with means
ggplot(dat, aes(x=rating, colour=sec)) +
geom_density() +
geom_vline(data=cdat, aes(xintercept=rating.mean,  colour=sec),
linetype="dashed", size=1)
# Using facets:
ggplot(dat, aes(x=rating)) +
geom_histogram(binwidth=.5, colour="black", fill="white") +
facet_grid(sec ~ .)
# With mean lines, using cdat from above
ggplot(dat, aes(x=rating)) +
geom_histogram(binwidth=.5, colour="black", fill="white") +
facet_grid(sec ~ .) +
geom_vline(data=cdat, aes(xintercept=rating.mean),
linetype="dashed", size=1, colour="red")
###----
# Box plots
###----
# A basic box plot
ggplot(dat, aes(x=sec, y=rating)) +
geom_boxplot()
# A basic box with the secitions colored
ggplot(dat, aes(x=sec, y=rating, fill=sec)) +
geom_boxplot()
# The above adds a redundant legend. With the legend removed:
ggplot(dat, aes(x=sec, y=rating, fill=sec)) +
geom_boxplot() +
guides(fill=FALSE)
# With flipped axes
ggplot(dat, aes(x=sec, y=rating, fill=sec)) + geom_boxplot() +
guides(fill=FALSE) +
coord_flip()
# It’s also possible to add the mean by using stat_summary.
# Add a diamond at the mean, and make it larger
ggplot(dat, aes(x=sec, y=rating)) +
geom_boxplot() +
stat_summary(fun.y=mean, geom="point", shape=22, size=4)
###----
# 2D Histogram | Heatmap
###----
# Basic 2D Histogram
s <- matrix(c(1, -.75, -.75, 1), ncol = 2)
obs <- mvtnorm::rmvnorm(500, sigma = s)
p <- plot_ly(x = obs[,1], y = obs[,2])
pp <- subplot(
p %>% add_markers(alpha = 0.2),
p %>% add_histogram2d()
)
pp
# Colorscale
p <- p %>% add_histogram2d(colorscale = "Blues")
p
# Z Matrix
cnt <- with(diamonds, table(cut, clarity))
cnt
p <- plot_ly(diamonds, x = ~cut, y = ~clarity, z = ~cnt) %>%
add_histogram2d()
p
# Date: 2017-06-20
# Source 1: https://plot.ly/r/getting-started/
# Source 2: https://plot.ly/r/
# install.packages("tidyverse")
# install.packages("plotly")                    # 1st variant
# devtools::install_github("ropensci/plotly")   # 2nd variant
library(tidyverse)
library(plotly)
# Boxplot
p <- plot_ly(midwest, x = ~percollege, color = ~state, type = "box")
p
