Advertisement
Guest User

pratica.R

a guest
Feb 19th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 3.53 KB | None | 0 0
  1. # O dataset utilizado será o Iris, disponibilizado pelo R
  2.  
  3. ?iris
  4.  
  5. class(iris)
  6.  
  7. head(iris)
  8.  
  9. # ESTATÍSTICA DESCRITIVA
  10.  
  11. # Somatório
  12.  
  13. sum(iris$Sepal.Length)
  14.  
  15. # Produtório
  16.  
  17. prod(iris$Sepal.Length)
  18.  
  19. # Média arítmética
  20.  
  21. mean(iris$Sepal.Length)
  22.  
  23. sum(iris$Sepal.Length)/length(iris$Sepal.Length)
  24.  
  25. mean(iris$Sepal.Length[iris$Species == "setosa"])
  26.  
  27. # Média Ponderada
  28.  
  29. seto <- mean(iris$Sepal.Length[iris$Species == "setosa"])
  30.  
  31. vers <- mean(iris$Sepal.Length[iris$Species == "versicolor"])
  32.  
  33. virg <-mean(iris$Sepal.Length[iris$Species == "virginica"])
  34.  
  35. (seto * 5 + vers * 3 + virg * 2)/10
  36.  
  37. # Mediana
  38.  
  39. median(iris$Sepal.Length)
  40.  
  41. # Moda
  42.  
  43. getmode <- function(v) {uniqv <- unique(v) uniqv[which.max(tabulate(match(v, uniqv)))]}
  44.  
  45. getmode(iris$Sepal.Width)
  46.  
  47. table(iris$Sepal.Width)
  48.  
  49. # Variância de Desvio Padrão
  50.  
  51. var(iris$Sepal.Length)
  52.  
  53. sd(iris$Sepal.Length)
  54.  
  55. sqrt(var(iris$Sepal.Length))
  56.  
  57. # Outras métricas
  58.  
  59. min(iris$Sepal.Length)
  60.  
  61. max(iris$Sepal.Length)
  62.  
  63. range(iris$Sepal.Length)
  64.  
  65. quantile(iris$Sepal.Length)
  66.  
  67. abs(iris$Sepal.Length)
  68.  
  69. sample(iris$Sepal.Length, 10)
  70.  
  71. summary(iris$Sepal.Length)
  72.  
  73. table(iris$Sepal.Length)
  74.  
  75. # Correlação
  76.  
  77. cor(iris$Sepal.Length,iris$Petal.Length)
  78.  
  79. cor(iris$Petal.Length,iris$Petal.Width)
  80.  
  81. cor(iris$Petal.Width,iris$Sepal.Width)
  82.  
  83. # GRÁFICOS
  84.  
  85. # line plots
  86.  
  87. plot(table(iris$Petal.Width), col='blue', type='o')
  88.  
  89. # Scatter plot
  90.  
  91. scatter.smooth(iris$Petal.Length~iris$Petal.Width,col=heat.colors(length(iris$Petal.Length)),pch=16)
  92.  
  93. #Bar plot
  94. barplot(table(iris$Sepal.Length),col=cm.colors(length(table(iris$Sepal.Length))))
  95.  
  96. # Pie plot
  97.  
  98. pie(head(table(iris$Petal.Length),10))
  99.  
  100. # Hisograma
  101.  
  102. hist(iris$Sepal.Length)
  103.  
  104. # Density plot
  105.  
  106. plot(density(iris$Sepal.Length))
  107.  
  108. # Boxplot
  109.  
  110. boxplot(iris)
  111.  
  112. summary(iris)
  113.  
  114. # Heatmap
  115. heatmap(t(head(iris[1:4], 30)))
  116.  
  117. ##
  118. ## ggplot2
  119. ##
  120.  
  121. library(ggplot2)
  122.  
  123. #scatter plot simples - camadas 1!
  124.  
  125. ggplot(data=iris, aes(x = Sepal.Length, y= Petal.Length))
  126.  
  127. #scatter plot simples - camadas 2!
  128. ggplot(data=iris, aes(x = Sepal.Length, y= Petal.Length)) + geom_point()
  129.  
  130. #scatter plot melhorado 1
  131.  
  132. ggplot(data=iris, aes(x = iris$Sepal.Length, y= iris$Petal.Length))+ geom_point(aes(col=iris$Species))
  133.  
  134. #scatter plot melhorado 2
  135.  
  136. ggplot(data=iris, aes(x = iris$Sepal.Length, y= iris$Petal.Length))+ geom_point(aes(col=iris$Species, shape=iris$Species))
  137.  
  138. #scatter plot melhorado 3
  139. ggplot(data=iris, aes(x = iris$Sepal.Length, y= iris$Petal.Length))+ geom_point(aes(col=iris$Species, shape=iris$Species)) + geom_smooth()
  140.  
  141.  
  142. #scatter plot separado
  143.  
  144. ggplot(data=iris, aes(x = iris$Sepal.Length, y= iris$Petal.Length))+ geom_point(aes(col=iris$Species, shape=iris$Species)) + facet_grid(. ~ Species)
  145.  
  146.  
  147. #boxplot
  148.  
  149. ggplot(data=iris, aes(x=iris$Species, y=iris$Sepal.Length)) + geom_boxplot(aes(fill=iris$Species))
  150.  
  151.  
  152. #Violin plot
  153.  
  154. ggplot(data=iris, aes(x=iris$Species, y=iris$Sepal.Length)) + geom_violin(aes(fill=iris$Species))
  155.  
  156. #Histograma
  157.  
  158. ggplot(data=iris, aes(x=iris$Sepal.Width)) + geom_histogram(binwidth=0.2, color="black", aes(fill=Species))
  159.  
  160. # Densidade
  161.  
  162. ggplot(data=iris, aes(x=Sepal.Width, fill=Species))+ geom_density(stat="density", alpha=(0.2))
  163.  
  164. # heatmap
  165.  
  166. library(reshape2)
  167.  
  168. dat <- iris[,1:4]
  169. cor <- melt(cor(dat, use="p"))
  170. head(cor)
  171.  
  172. ggplot(data=cor, aes(x=Var1, y=Var2, fill=value)) + scale_fill_gradient2(limits=c(-1, 1))
  173.  
  174. #Adicionando rótulos
  175. ggplot(data=cor, aes(x=Var1, y=Var2, fill=value)) + scale_fill_gradient2(limits=c(-1, 1)) + geom_tile()+ geom_text(aes(label=round(value,3))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement