SHARE
TWEET

Untitled

a guest Sep 24th, 2014 50 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. readfile <- function(lv, name)
  3. {
  4.  f <- read.csv(paste("C:/Piratistiken-master/Piratistiken-master/", name, sep=""),
  5.                sep="\t", comment.char = "#", header=F, col.names=c("Date", paste("M.", lv, sep=""), paste("S.", lv, sep=""), "x"))
  6.  f$Date <- as.Date(sprintf("%s", t(f$Date)), "%Y%m%d")
  7.  f[,1:3]
  8. }
  9.  
  10. m <- function(ar1, ar2)
  11. {
  12.  merge(ar1, ar2, by="Date", all=T)
  13. }
  14.  
  15.  
  16. data <- readfile("BB", "LV-BB.txt")
  17. data <- m(data, readfile("BE", "LV-BE.txt"))
  18. data <- m(data, readfile("BW", "LV-BW.txt"))
  19. data <- m(data, readfile("BY", "LV-BY.txt"))
  20. data <- m(data, readfile("HB", "LV-HB.txt"))
  21. data <- m(data, readfile("HE", "LV-HE.txt"))
  22. data <- m(data, readfile("HH", "LV-HH.txt"))
  23. data <- m(data, readfile("LSA", "LV-LSA.txt"))
  24. data <- m(data, readfile("MV", "LV-MV.txt"))
  25. data <- m(data, readfile("NDS", "LV-NDS.txt"))
  26. data <- m(data, readfile("NRW", "LV-NRW.txt"))
  27. data <- m(data, readfile("RP", "LV-RP.txt"))
  28. data <- m(data, readfile("SH", "LV-SH.txt"))
  29. data <- m(data, readfile("SL", "LV-SL.txt"))
  30. data <- m(data, readfile("SN", "LV-SN.txt"))
  31. data <- m(data, readfile("TH", "LV-TH.txt"))
  32. data <- m(data, readfile("BV", "OUTSIDE.txt"))
  33.  
  34. require(zoo)
  35. datafull <- data
  36. for (c in (1:((dim(datafull)[2]-3)/2)*2+1)) {
  37.  datafull[,c] <- ifelse(datafull[,c]<10, NA, datafull[,c])
  38. }
  39. for (c in (2:dim(datafull)[2])) {
  40.  datafull[,c] <- na.locf(datafull[,c], na.rm=F)
  41. }
  42.  
  43. datafull$M <- apply(datafull[,(1:17)*2], 1, sum)
  44. datafull$S <- apply(datafull[,(1:17)*2+1], 1, sum)
  45.  
  46. trend <- function(data, l1, l2)
  47. {
  48.   abline(b=  (data$S[l2]-data$S[l1]) / (as.numeric(data$Date[l2])-as.numeric(data$Date[l1])),
  49.          a=-((data$S[l2]-data$S[l1]) / (as.numeric(data$Date[l2])-as.numeric(data$Date[l1]))) * as.numeric(data$Date[l1]) + data$S[l1],
  50.          col=rgb(0,0,0,0.5))
  51. }
  52.  
  53. predict <- function(data, l1, l2, d)
  54. {
  55.  slope <- (data$S[l2]-data$S[l1]) / (as.numeric(data$Date[l2])-as.numeric(data$Date[l1]))
  56.  inter <- -((data$S[l2]-data$S[l1]) / (as.numeric(data$Date[l2])-as.numeric(data$Date[l1]))) * as.numeric(data$Date[l1]) + data$S[l1]
  57.  inter + slope * as.numeric(as.Date(d))
  58. }
  59.  
  60. plot(S ~ Date, data=datafull, type="l", ylim=c(0,20000), xlim=as.Date(c("2012-11-18", "2014-12-31")), xlab="", xaxt="n", col="orange", lwd=2, main="Piratenpartei Deutschland, zahlende Mitglieder, bundesweit")
  61. #abline(v=as.Date(c("2013-01-01", "2014-01-01", "2015-01-01")), col=rgb(0, 0, 0, 0.5))
  62. abline(v=as.Date(c("2014-01-01", "2015-01-01")), col=rgb(0, 0, 0, 0.5))
  63. dateaxis <- as.Date(sprintf("%4d-%02d-01", rep(2006:2015, each=12), rep(1:12,10)))
  64. axis(1, dateaxis, format(dateaxis, "%b %Y"), cex.axis = .7, las=2)
  65.  
  66. trend(datafull, 1114, 1261)
  67. trend(datafull, 1266, 1407)
  68. lines(x=c(0, as.numeric(as.Date("2013-12-31"))), y=rep(datafull$S[1261],2), col=rgb(0,0,0,0.5), lwd=2)
  69. lines(x=c(0, as.numeric(as.Date("2014-12-31"))), y=rep(predict(datafull, 1266, 1407, "2014-12-31"),2), col=rgb(0,0,0,0.5), lwd=2)
  70. points(x=as.Date(c("2013-12-31", "2014-12-31")), y=c(datafull$S[1261],predict(datafull, 1266, 1407, "2014-12-31")), col=rgb(0,0,0,0.5), cex=1.5, pch=0, lwd=2)
  71.  
  72. lines(x=c(0, rep(as.numeric(as.Date("2013-09-23")),2)), y=c(rep(datafull$S[1201],2),-10000), col=rgb(0.2,0.2,1,0.5), lwd=2)
  73. lines(x=c(0, rep(as.numeric(as.Date("2014-09-23")),2)), y=c(rep(datafull$S[dim(datafull)[1]],2),-10000), col=rgb(0.2,0.2,1,0.5), lwd=2)
  74. points(x=as.Date(c("2013-09-23", "2014-09-23")), y=c(datafull$S[1201],datafull$S[dim(datafull)[1]]), col=rgb(0.2,0.2,1,0.5), cex=1.7, pch=1, lwd=2)
  75. legend("topright", col=c("orange","black","blue"), pch=c(NA, 0, 1), cex=1.2, lwd=2, bty="n", legend=c("zahlende Mitglieder", "Vergleich Jahresendwerte", "Periodenvergleich"))
  76.  
  77. ############################
  78.  
  79. plot(S / M ~ Date, data=datafull, type="l", ylim=c(0,0.6), xlim=as.Date(c("2012-11-18", "2014-12-31")), ylab="Zahlerquote", xlab="", xaxt="n", col="orange", lwd=2, main="Piratenpartei Deutschland, Zahlerquote, bundesweit", las=2)
  80. abline(v=as.Date(c("2013-01-01", "2014-01-01", "2015-01-01")), col=rgb(0, 0, 0, 0.5))
  81. dateaxis <- as.Date(sprintf("%4d-%02d-01", rep(2006:2015, each=12), rep(1:12,10)))
  82. axis(1, dateaxis, format(dateaxis, "%b %Y"), cex.axis = .7, las=2)
RAW Paste Data
Top