Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pdf("fig_09.pdf", width = 8, height = 5)
- par(bty = "n", las = 1, mar = c(3, 6, 1, 1) + 0.2, mgp = c(5, 1, 0))
- # función suma armónicos
- sum_arm <- function(n) {
- a <- numeric(n)
- for (i in 1:n) {
- for (j in 1:i) {
- a[j] <- a[j] + 1/i
- }
- }
- a/n
- }
- # función para contar la ocurrencia de una letra
- cuentaletras <- function(letra, texto) {
- texto2 <- gsub(letra, "", texto, ignore.case = TRUE)
- return (nchar(texto) - nchar(texto2))
- }
- # cargamos el libro en una cadena gigante
- libro <- readChar("quijote.txt", file.info("quijote.txt")$size)
- # quitamos saltos de línea
- libro <- gsub("\n", "", libro)
- # quitamos las tildes y diéresis
- libro <- chartr("áéíóúàèìòùäëïöüÁÉÍÓÚÀÈÌÒÙÄËÏÖÜ", "aeiouaeiouaeiouAEIOUAEIOUAEIOU", libro)
- # abecedario
- abecedario <- "abcdefghijklmnñopqrstuvwxyz"
- # contaje y resultado
- for (i in 1:27) {
- letra <- substr(abecedario, i, i)
- cuenta <- cuentaletras(letra, libro)
- cat(letra, ": ", cuenta, "\n")
- }
- # datos
- obs <- c(229147, 200466, 162480, 125701, 108415, 100932, 90055, 89125, 87230, 79550, 61734, 59425, 44649, 35451, 32478, 25107, 24142, 19916, 17855, 17224, 10530, 7581, 6490, 4241, 377, 2)
- nomb <- c("e", "a", "o", "s", "n", "r", "i", "l", "d", "u", "t", "c", "m", "p", "q", "y", "b", "h", "v", "g", "j", "f", "z", "ñ", "x", "w")
- n <- length(obs)
- teor <- sum(obs)*sum_arm(n)
- R2 <- 1 - sum((obs - teor)^2) / var(obs) / (n - 1)
- # gráfico
- plot(1:n - 0.1, obs, ylab = "Número de veces", type = "h", las = 2, ylim = c(0, 250000), lwd = 2, xlab = "", xaxt = "n")
- axis(1, at = 1:n, labels = nomb, las = 1, cex.axis = 0.8)
- lines(1:n + 0.1, teor, type = "h", col = "red", lwd = 2)
- legend("topright", bty = "n", cex = 0.8, c("observado", "teórico"), lty = 1, lwd = 2, col = c("black", "red"))
- text(16, 160000, bquote(R^2 ~ ":" ~ .(round(R2, 2))))
- dev.off()
Advertisement
Add Comment
Please, Sign In to add comment