Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # O dataset utilizado será o Iris, disponibilizado pelo R
- ?iris
- class(iris)
- head(iris)
- # ESTATÍSTICA DESCRITIVA
- # Somatório
- sum(iris$Sepal.Length)
- # Produtório
- prod(iris$Sepal.Length)
- # Média arítmética
- mean(iris$Sepal.Length)
- sum(iris$Sepal.Length)/length(iris$Sepal.Length)
- mean(iris$Sepal.Length[iris$Species == "setosa"])
- # Média Ponderada
- seto <- mean(iris$Sepal.Length[iris$Species == "setosa"])
- vers <- mean(iris$Sepal.Length[iris$Species == "versicolor"])
- virg <-mean(iris$Sepal.Length[iris$Species == "virginica"])
- (seto * 5 + vers * 3 + virg * 2)/10
- # Mediana
- median(iris$Sepal.Length)
- # Moda
- getmode <- function(v) {uniqv <- unique(v) uniqv[which.max(tabulate(match(v, uniqv)))]}
- getmode(iris$Sepal.Width)
- table(iris$Sepal.Width)
- # Variância de Desvio Padrão
- var(iris$Sepal.Length)
- sd(iris$Sepal.Length)
- sqrt(var(iris$Sepal.Length))
- # Outras métricas
- min(iris$Sepal.Length)
- max(iris$Sepal.Length)
- range(iris$Sepal.Length)
- quantile(iris$Sepal.Length)
- abs(iris$Sepal.Length)
- sample(iris$Sepal.Length, 10)
- summary(iris$Sepal.Length)
- table(iris$Sepal.Length)
- # Correlação
- cor(iris$Sepal.Length,iris$Petal.Length)
- cor(iris$Petal.Length,iris$Petal.Width)
- cor(iris$Petal.Width,iris$Sepal.Width)
- # GRÁFICOS
- # line plots
- plot(table(iris$Petal.Width), col='blue', type='o')
- # Scatter plot
- scatter.smooth(iris$Petal.Length~iris$Petal.Width,col=heat.colors(length(iris$Petal.Length)),pch=16)
- #Bar plot
- barplot(table(iris$Sepal.Length),col=cm.colors(length(table(iris$Sepal.Length))))
- # Pie plot
- pie(head(table(iris$Petal.Length),10))
- # Hisograma
- hist(iris$Sepal.Length)
- # Density plot
- plot(density(iris$Sepal.Length))
- # Boxplot
- boxplot(iris)
- summary(iris)
- # Heatmap
- heatmap(t(head(iris[1:4], 30)))
- ##
- ## ggplot2
- ##
- library(ggplot2)
- #scatter plot simples - camadas 1!
- ggplot(data=iris, aes(x = Sepal.Length, y= Petal.Length))
- #scatter plot simples - camadas 2!
- ggplot(data=iris, aes(x = Sepal.Length, y= Petal.Length)) + geom_point()
- #scatter plot melhorado 1
- ggplot(data=iris, aes(x = iris$Sepal.Length, y= iris$Petal.Length))+ geom_point(aes(col=iris$Species))
- #scatter plot melhorado 2
- ggplot(data=iris, aes(x = iris$Sepal.Length, y= iris$Petal.Length))+ geom_point(aes(col=iris$Species, shape=iris$Species))
- #scatter plot melhorado 3
- ggplot(data=iris, aes(x = iris$Sepal.Length, y= iris$Petal.Length))+ geom_point(aes(col=iris$Species, shape=iris$Species)) + geom_smooth()
- #scatter plot separado
- ggplot(data=iris, aes(x = iris$Sepal.Length, y= iris$Petal.Length))+ geom_point(aes(col=iris$Species, shape=iris$Species)) + facet_grid(. ~ Species)
- #boxplot
- ggplot(data=iris, aes(x=iris$Species, y=iris$Sepal.Length)) + geom_boxplot(aes(fill=iris$Species))
- #Violin plot
- ggplot(data=iris, aes(x=iris$Species, y=iris$Sepal.Length)) + geom_violin(aes(fill=iris$Species))
- #Histograma
- ggplot(data=iris, aes(x=iris$Sepal.Width)) + geom_histogram(binwidth=0.2, color="black", aes(fill=Species))
- # Densidade
- ggplot(data=iris, aes(x=Sepal.Width, fill=Species))+ geom_density(stat="density", alpha=(0.2))
- # heatmap
- library(reshape2)
- dat <- iris[,1:4]
- cor <- melt(cor(dat, use="p"))
- head(cor)
- ggplot(data=cor, aes(x=Var1, y=Var2, fill=value)) + scale_fill_gradient2(limits=c(-1, 1))
- #Adicionando rótulos
- 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