Question

In: Statistics and Probability

Write an R function that conducts a normality test as follows: it takes as input a...

Write an R function that conducts a normality test as follows: it takes as input a data set, calculates a bootstrap confidence interval for the skewness, calculates a bootstrap confidence interval for the kurtosis, then sees if 0 is in the skewness interval and 3 is in the kurtosis interval. If so, your routine prints that the data is normally distributed, otherwise your routine should print that the data is not normally distributed. Test your routine on random data from normal (rnorm), uniform (runif), and exponential (rexp) , with sample sizes of n=10,30,70 and 100.

- The R package moments gives us two very useful functions; skewness and kurtosis. If data is truly normal, it should have a skewness value of 0 and a kurtosis value of 3.

Solutions

Expert Solution

R code with comments

-------------

#load the library moments
library(moments)

#function to test
testNormal<-function(data){
   #set the number of bootstrap resamples
   B<-10000
   n<-length(data)
   #initialize the variables to hold skewness, kurtosis
   xskew<-numeric(B)
   xkurt<-numeric(B)
   #start the bootstrap loop
   for (i in 1:B) {
   #resample with replacement
   s<-sample(data,size=n,replace=TRUE)
   #calculate the sample skewness and kurtosis
   xskew[i]<-skewness(s)
   xkurt[i]<-kurtosis(s)
   }
   #get the 95% percentile interval for skewness,kurtosis
   ciskew<-quantile(xskew,c(0.025,0.975))
   cikurt<-quantile(xkurt,c(0.025,0.975))
   #check if normal
   if ((ciskew[1]<=0 & ciskew[2]>=0) & (cikurt[1]<= 3 &cikurt[2]>=3)){
   return('The data is normally distributed')
   } else {
   return('the data is not normally distributed')
   }
}


#set the random seed
set.seed(123)

#try for different sample sizes
sprintf('--->Normal data')
for (n in c(10,30,70,100)){
   #test data from normal
   out<-testNormal(rnorm(n))
   print(sprintf('For sample size n=%g: %s',n,out))
}

sprintf('--->Uniform data')
for (n in c(10,30,70,100)){
   #test data from uniform
   out<-testNormal(runif(n))
   print(sprintf('For sample size n=%g: %s',n,out))
}

sprintf('--->Exponential data')
for (n in c(10,30,70,100)){
   #test data from exponential
   testNormal(rexp(n))
   print(sprintf('For sample size n=%g: %s',n,out))
}
-------

#get this


Related Solutions

In R studio Write a function that takes as an input a positive integer and uses...
In R studio Write a function that takes as an input a positive integer and uses the print() function to print out all the numbers less than the input integer. (Example: for input 5, the function should print the numbers 1,2,3,4 { for input 1, the function should not print a number.) Use the lapply function, do not use any of the loop commands in your code.
Write a function redact() takes as input a file name. See secret.txt for the initial test....
Write a function redact() takes as input a file name. See secret.txt for the initial test. The function should print the contents of the file on the screen with this modification: Every occurrence of string 'secret' in the file should be replaced with string 'xxxxxx'. Every occurrence of “agent’ should be replaced with ‘yyyyyy’. Every occurrence of carrier should be replaced with ‘zzzzzz’. This function should catch exceptions. FOR PYTHON
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
write a function that takes as input the root of a general tree and returns a...
write a function that takes as input the root of a general tree and returns a binary tree generated by the conversion process illustrated in java
Write a function that takes a person’s first and last names as input and (a) uses...
Write a function that takes a person’s first and last names as input and (a) uses lists to return a list of the common letters in the first and last names (the intersection). (b) uses sets to return a set that is the intersection of the characters in the first and last names. (c) uses sets to return the set that is the symmetric difference between the first and last names. please write in python program
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or...
A. Write a function in MATLAB called findTranspose that takes in as input any matrix or any vector and simply returns back the transpose of that input. You can always verify your answer by using the inbuilt transpose function in MATLAB, however, you cannot use the transpose function directly when writing your code. Here is the starter code that we are providing to help you get started %x is the input vector or matrix. You can find the %size that...
USING PYTHON, write a function that takes a list of integers as input and returns a...
USING PYTHON, write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2]. DO NOT use any special or built in functions like append, reverse etc.
Write a function in Matlab that takes as input the number n and a symmetric tridiagonal...
Write a function in Matlab that takes as input the number n and a symmetric tridiagonal matrix given as two vectors: n×1 vector v representing the main diagonal and (n−1)×1 vector w representing the upper diagonal. Have this function output the Cholesky factor of the matrix as a vector for the main diagonal and a vector for the upper diagonal and output the number of flops and, separately, the number of square roots used as well. Use only basic programming....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT