# testing normality

#### ivn

##### New Member
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? 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
}
}

#### ivn

##### New Member
In addition to the previous code, I tried to do a different approach, but something still isn't working. I would be really thankful if someone would say what I am doing wrong:
Code:
result <- shapiro.test(dat)
skew <- skewness(dat)

if(result$p.value>0.05) { "The distribution is normal" print(histogram) } else if(result$p.value<0.05 && skew < (-0.5))
{
display <- sprintf("The curve is shifted to the right, skewness = %f", skew)
print(display)
print (histogram)
}else (result\$p.value<0.05 && skew> 0.5)
{
display<- sprintf("The curve is shifted to the left, skewness = %f", skew)
print(histogram)
print(display)
}