To load or find the datasets, we have to authentify using the API like with Twitter First, we need to set an account to receive the Quandl.auth
setwd("D:/PERSO/R_Working/Tutoriels/Probability")
library(Quandl)
Quandl.auth("XXXXXXXXXX") ### Replace with yours
For example, we load the database about pollution and gdp and try to find the link.plot(date, pollution, col = "red", type = "o", lwd = 2, ylim = c(70, 150), ylab = "",
main = "Evolution du PIB vs Pollution entre 94 et 2006 au Japon")
lines(date, pib, lty = 2, col = "purple", type = "o")
legend("topright", legend = c("PIB", "Pollution"), col = c("purple", "red"),
pch = 15, bty = "n", pt.cex = 2, cex = 0.8, text.col = "black", horiz = TRUE,
inset = c(0.1, 0.1))
We fit a model to know if the growth of GDP is responsible of pollution's growth in japan between 1994 and 2006.
Ecologists are not totaly wrong ! ! !
The entire code to run this post
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lm_eqn = function(df) { | |
m = lm(pollution ~ pib, df); | |
l <- list(a = format(coef(m)[1], digits = 2), | |
b = format(abs(coef(m)[2]), digits = 2), | |
r2 = format(summary(m)$r.squared, digits = 3)); | |
if (coef(m)[2] >= 0) { | |
eq <- substitute(italic(Pollution) == a + b %.% italic(PIB)*","~~italic(R)^2~"="~r2,l) | |
} else { | |
eq <- substitute(italic(Pollution) == a - b %.% italic(PIB)*","~~italic(R)^2~"="~r2,l) | |
} | |
as.character(as.expression(eq)); | |
} | |
require(ggplot2) | |
p=ggplot(dd, aes(x=pib, y=pollution)) +geom_point(shape=5) ; | |
p1 = p + geom_text(aes(x =110, y = 110, label = lm_eqn(dd)), parse = TRUE)+ | |
geom_smooth(method=lm) | |
print(p1) |
Share it ! ! !
No comments:
Post a Comment