[R] The qurey about kolmogorov-smirnov test & adding the trendline to graph

priti desai priti.desai at kalyptorisk.com
Fri Jun 16 09:26:33 CEST 2006


I am hereby forwarding the data & method use to calculate the
Kolmogorov-Smirnov goodness of fit test made manually by me in R
launguage which deffers with the actual inbuilt formula as  shown below.
Further I have plot the graph in R. In that graph how to add trendline
(i.e. straight line passing through maximum points in plot) to a Plot.


R script is as follows please run this script to see the output in R.

========================= start ========================= 

# The data is as follows

data <- c( 0.01,  0.02, 0.04, 0.13,  0.17 , 0.19 , 0.21 , 0.27 , 0.27 ,
0.28,  0.29,  0.37,
           0.41,  0.49,  0.51,  0.52,  0.54,  0.57,  0.62,  0.63,  0.68,
0.73,  0.74, 0.79,
           0.81,  0.81,  0.82,  0.86,  0.94,  0.96,  1.02,  1.10,  1.10,
1.20,  1.29,  1.36,
           1.40,  1.41,  1.44,  1.45,  1.62,  1.67,  1.69,  1.78,  1.82,
2.11,  2.13,  2.14,
           2.24,  2.29,  2.34, 2.40,  2.46,  2.70,  2.83,  2.98,  3.00,
3.30,  3.53,  3.70,
           3.86,  3.90,  3.91,  3.98,  5.01,  5.23,  6.05,  6.12, 10.41,
10.73)



# K-S goodness of fit test (actual calculated)
print('K-S goodness of fit test')

# Ho:sample come from postulated prob distribution  vs   H1: Not Ho
# Reject Ho if o > 0.162312045 o.w. don't reject Ho at 5% los.
# Reject Ho if o > 0.194583 o.w. don't reject Ho at 1% los.
# In this case, we don't Reject Ho at 5% &1% los.

average       <- mean(data)  
print('average:')
# estimate the parameter
print('Estimation of parameter')

lambda        <- (1/average)
print(lambda)


e             <- c(1:70)
k             <- c((e-1)/70)
Fx            <- c(1 - exp(-lambda*data))
g             <- sort(Fx)
l             <- c(g-k)
m             <- c(e/70)
n             <- c(m-g)                        
o             <- max(l,n)
print('k-s stat:')
print(o)

#  K-S goodness of fit test (R inbuilt formula)


 ks.test(x= data,"pexp", alternative = c("g"),exact = NULL)



# P-P plot

 
e             <- c(1:70)
f             <- c((e-.5)/70)
Fx            <- c(1 - exp(-lambda*data))
g             <- sort(Fx)
plot(f,g)

========================= end ========================= 

The results are as follows:
  The K-S test calculated manualy giving the result as follows,
 
Ho:sample come from postulated prob distribution  vs   H1: Not Ho
Ks statistic = D = 0.04391726

 Reject Ho if o > 0.162312045 o.w. don't reject Ho at 5% los.
 Reject Ho if o > 0.194583 o.w. don't reject Ho at 1% los.
 In this case, we don't Reject Ho at 5% &1% los.

 (by inbuilt formula the output is as follows,)

 One-sample Kolmogorov-Smirnov test

data:  data 
D^+ = 0.0088, p-value = 0.9893
alternative hypothesis: greater 

Warning message:
cannot compute correct p-values with ties in: ks.test(x = data, "pexp",
alternative = c("g"), exact = NULL) 




It is requested to clarify & confirm formula derirved so as to enable 
me to cross check my calculations made manually. Further please convey 
as how to interprete the results. 
Awaiting your positive reply.

 
Regards,
Priti



More information about the R-help mailing list