JuliaForDataAnalysis/ch07.jl

215 lines
3.5 KiB
Julia
Raw Normal View History

2022-01-19 18:22:20 +01:00
# Bogumił Kamiński, 2022
2022-02-08 20:58:33 +01:00
# Codes for chapter 5
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
# Code for listing 5.1
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
using HTTP
using JSON3
query = "https://api.nbp.pl/api/exchangerates/rates/a/usd/" *
"2020-06-01/?format=json"
response = HTTP.get(query)
json = JSON3.read(response.body)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
# Code for the remainder of section 5.1.2
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
response.body
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
String(response.body)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
response.body
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
json.table
json.currency
json.code
json.rates
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
json.rates[1].mid
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
only(json.rates).mid
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
only([])
only([1, 2])
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
# Code for listing 5.2
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
query = "https://api.nbp.pl/api/exchangerates/rates/a/usd/" *
"2020-06-06/?format=json"
response = HTTP.get(query)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
# Code for listing 5.3
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
query = "https://api.nbp.pl/api/exchangerates/rates/a/usd/" *
"2020-06-01/?format=json"
try
response = HTTP.get(query)
json = JSON3.read(response.body)
only(json.rates).mid
catch e
if e isa HTTP.ExceptionRequest.StatusError
missing
else
rethrow(e)
end
end
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
query = "https://api.nbp.pl/api/exchangerates/rates/a/usd/" *
"2020-06-06/?format=json"
try
response = HTTP.get(query)
json = JSON3.read(response.body)
only(json.rates).mid
catch e
if e isa HTTP.ExceptionRequest.StatusError
missing
else
rethrow(e)
end
end
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
# Code for section 5.2
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
ismissing(missing)
ismissing(1)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
1 + missing
sin(missing)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
1 == missing
1 > missing
1 < missing
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
if missing
print("this is not printed")
end
missing && true
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
coalesce(missing, true)
coalesce(missing, false)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
isequal(1, missing)
isequal(missing, missing)
isless(1, missing)
isless(missing, missing)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
isless(Inf, missing)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
a = [1]
b = [1]
isequal(a, b)
a === b
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
x = [1, missing, 3, 4, missing]
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
coalesce.(x, 0)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
sum(x)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
y = skipmissing(x)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
sum(y)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
sum(skipmissing(x))
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
fun(x::Int, y::Int) = x + y
fun(1, 2)
fun(1, missing)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
using Missings
fun2 = passmissing(fun)
fun2(1, 2)
fun2(1, missing)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
# Code for section 5.3
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
using Dates
d = Date("2020-06-01")
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
typeof(d)
year(d)
month(d)
day(d)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
dayofweek(d)
dayname(d)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
Date(2020, 6, 1)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
dates = Date.(2020, 6, 1:30)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
Day(1)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
d
d + Day(1)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
Date(2020, 5, 20):Day(1):Date(2020, 7, 5)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
collect(Date(2020, 5, 20):Day(1):Date(2020, 7, 5))
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
# Code for listing 5.6
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
function get_rate(date::Date)
query = "https://api.nbp.pl/api/exchangerates/rates/" *
"a/usd/$date/?format=json"
try
response = HTTP.get(query)
json = JSON3.read(response.body)
return only(json.rates).mid
catch e
if e isa HTTP.ExceptionRequest.StatusError
return missing
else
rethrow(e)
end
2022-01-19 18:22:20 +01:00
end
end
2022-02-08 20:58:33 +01:00
# Code for showing how string interpolation works
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
"https://api.nbp.pl/api/exchangerates/rates/" *
"a/usd/$(dates[1])/?format=json"
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
"https://api.nbp.pl/api/exchangerates/rates/" *
"a/usd/$dates[1]/?format=json"
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
# Code for listing 5.7
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
rates = get_rate.(dates)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
# Code for section 5.4
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
using Statistics
mean(rates)
std(rates)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
mean(skipmissing(rates))
std(skipmissing(rates))
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
# Code for listing 5.8
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
using FreqTables
proptable(dayname.(dates), ismissing.(rates); margins=1)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
# Code showing how to specify a complex condition using broadcasting
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
dayname.(dates) .== "Thursday" .&& ismissing.(rates)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
# Code for listing 5.9
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
dates[dayname.(dates) .== "Thursday" .&& ismissing.(rates)]
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
# Codes for plotting exchange rate data
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
using Plots
plot(dates, rates; xlabel="day", ylabel="PLN/USD", legend=false)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
rates_ok = .!ismissing.(rates)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
plot(dates[rates_ok], rates[rates_ok];
xlabel="day", ylabel="PLN/USD", legend=false)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
using Impute
rates_filled = Impute.interp(rates)
2022-01-19 18:22:20 +01:00
2022-02-08 20:58:33 +01:00
scatter!(dates, rates_filled)