Can someone help to find the error in my code? I wanted to make a program in R which will tell me if a distribution is normal or not. It should also print a histogram with a density curve. After reading the .csv file and required packages (moments and ggplot2) i test my data for normality with the shapiro wilks test (normality <- shapiro.test() ) . After the testing, the p value is being obtained for usage in the if function (normality$p.value<0.05).
The if function should go somewhat like this:
If the p value is smaller than 0,05 test the skewness. If the skewness is less than -0,5 the distribution is shifted into the right and print a histogram of it. If the skewness is higher than >0.5, the distribution is shifted into the left, print a histogram. If the skewness is between 0,5 and -0,5 the distribution is aproximately simetrical. Else, the distribution is normal.
After running my code, nothing happens. Has somebody suggestion what might be wrong? Has anyone a better solution for this code?
The if function should go somewhat like this:
If the p value is smaller than 0,05 test the skewness. If the skewness is less than -0,5 the distribution is shifted into the right and print a histogram of it. If the skewness is higher than >0.5, the distribution is shifted into the left, print a histogram. If the skewness is between 0,5 and -0,5 the distribution is aproximately simetrical. Else, the distribution is normal.
After running my code, nothing happens. Has somebody suggestion what might be wrong? Has anyone a better solution for this code?
Code:
library("moments")
library("ggplot2")
histogram <- qplot(data, binwidth = 1.0, geom = "histogram", xlab = "my data",
ylab = "data frequency",
y = ..density.., fill = I("salmon"), colour = I("black")) +
stat_density(geom = "line")
normality <- shapiro.test(data)
skew <- skewness(data)
if(normality$p.value<0.05){
if(skew< -0.5){
"The distribution is shifted into the right"
histogram
}
if(skew> 0.5){
"The distribution is shifted into the left"
histogram
}
if(skew<=0.5 && skew>=-0.5){
"The distribution is simetrical"
return()
}
else{
"The distribution is normal"
histogram
}
}