#================================================================================================================# #========== Jacek Wallusch ==========# #========== MATHEMATICAL STATISTICS ==========# #========== Lecture 5: Empirical Probability Distribution: Histogram and Kernel Density ==========# #================================================================================================================# #----------------------------------------------------------------------------------------------------------------- # THE DATA: # Federal Reserve Bank of New York: https://www.newyorkfed.org/microeconomics/sceindex # variables: [1] Percent chance 12 months from now unemployment rate higher # [2] Percent chance 12 months from now interest rate on savings account higher # time: January 2016 # WARNING: don't forget to change the location of the file!!! us_exp <- read.csv("C:/Documents and Settings/User/Moje dokumenty/WALLUSCH/Wallusch-Datenbank.de/matstat/fedny.csv", header = TRUE, sep = ";") q4_fed <- us_exp[,1] q5_fed <- us_exp[,2] #----------------------------------------------------------------------------------------------------------------- # HISTOGRAM: # basic graph hist(q5_fed) # density plotted against the bins hist(q4_fed, freq = FALSE) # extracting the data: [1] counts for bins [2] densities for bins [3] midpoints for bins hist(q4_fed)$counts hist(q4_fed)$density hist(q4_fed)$mids #----------------------------------------------------------------------------------------------------------------- # KERNEL DENSITY: # summary table density(q4_fed) # basic graph plot(density(q4_fed)) # extracting the data density(q4_fed)$x density(q4_fed)$y #----------------------------------------------------------------------------------------------------------------- # slightly nicer plot # package: PLOTLY library(plotly) # graph: plot_ly(x = density(q4_fed)$x, y = density(q4_fed)$y, type = "scatter", mode = "lines")%>% layout(xaxis = list(title = "values"), yaxis = list(title = "density"))