enero 06, 2013

Prueba de normalidad en R


#####
# Prueba de Normalidad en R
# Respuesta de maí­z a fertilización nitrogenada en 5 lugares

# Cargando agridat, grid, y reshape 2
library(agridat)
Loading required package: grid
Loading required package: lattice
Loading required package: reshape2
library(grid)
library(reshape2)

# Datos a utilizar
?hernandez.nitrogen
starting httpd help server ...
done
data(hernandez.nitrogen)
hernandez.nitrogen
    site       loc rep nitro  yield
1     S1 Faribault  R1   0.0  3.697
2     S1 Faribault  R2   0.0  3.878
3     S1 Faribault  R3   0.0  4.389
4     S1 Faribault  R4   0.0  4.639
5     S1 Faribault  R1  33.6  6.899
6     S1 Faribault  R2  33.6  8.760
7     S1 Faribault  R3  33.6  7.468
8     S1 Faribault  R4  33.6  8.955
9     S1 Faribault  R1  67.2  9.369
10    S1 Faribault  R2  67.2 10.444
11    S1 Faribault  R3  67.2  9.800
12    S1 Faribault  R4  67.2 11.767
13    S1 Faribault  R1 100.8 11.522
14    S1 Faribault  R2 100.8 12.939
15    S1 Faribault  R3 100.8 12.199
16    S1 Faribault  R4 100.8 11.291
17    S1 Faribault  R1 134.4 11.496
18    S1 Faribault  R2 134.4 12.182
19    S1 Faribault  R3 134.4 10.683
20    S1 Faribault  R4 134.4 11.164
21    S1 Faribault  R1 168.0 11.827
22    S1 Faribault  R2 168.0 10.349
23    S1 Faribault  R3 168.0 11.611
24    S1 Faribault  R4 168.0  9.655
25    S1 Faribault  R1 201.6 11.067
26    S1 Faribault  R2 201.6 11.690
27    S1 Faribault  R3 201.6 11.152
28    S1 Faribault  R4 201.6  9.928
29    S2  Fillmore  R1   0.0  7.733
30    S2  Fillmore  R2   0.0  7.465
31    S2  Fillmore  R3   0.0  8.780
32    S2  Fillmore  R4   0.0  9.948
33    S2  Fillmore  R1  33.6  9.412
34    S2  Fillmore  R2  33.6  9.790
35    S2  Fillmore  R3  33.6  9.753
36    S2  Fillmore  R4  33.6 10.012
37    S2  Fillmore  R1  67.2 11.751
38    S2  Fillmore  R2  67.2 11.293
39    S2  Fillmore  R3  67.2 11.606
40    S2  Fillmore  R4  67.2 12.009
41    S2  Fillmore  R1 100.8 11.827
42    S2  Fillmore  R2 100.8 10.965
43    S2  Fillmore  R3 100.8 13.486
44    S2  Fillmore  R4 100.8 12.581
45    S2  Fillmore  R1 134.4 12.195
46    S2  Fillmore  R2 134.4 11.061
47    S2  Fillmore  R3 134.4 13.004
48    S2  Fillmore  R4 134.4 13.814
49    S2  Fillmore  R1 168.0 12.543
50    S2  Fillmore  R2 168.0 12.873
51    S2  Fillmore  R3 168.0 12.899
52    S2  Fillmore  R4 168.0 12.984
53    S2  Fillmore  R1 201.6 11.673
54    S2  Fillmore  R2 201.6 13.400
55    S2  Fillmore  R3 201.6 12.497
56    S2  Fillmore  R4 201.6 14.226
57    S3  Waseca 1  R1   0.0  9.499
58    S3  Waseca 1  R2   0.0 10.057
59    S3  Waseca 1  R3   0.0  8.027
60    S3  Waseca 1  R4   0.0  6.648
61    S3  Waseca 1  R1  33.6 10.015
62    S3  Waseca 1  R2  33.6 11.034
63    S3  Waseca 1  R3  33.6  9.466
64    S3  Waseca 1  R4  33.6  9.324
65    S3  Waseca 1  R1  67.2 13.317
66    S3  Waseca 1  R2  67.2 11.125
67    S3  Waseca 1  R3  67.2 11.619
68    S3  Waseca 1  R4  67.2 11.350
69    S3  Waseca 1  R1 100.8 12.243
70    S3  Waseca 1  R2 100.8 12.699
71    S3  Waseca 1  R3 100.8 11.522
72    S3  Waseca 1  R4 100.8 13.062
73    S3  Waseca 1  R1 134.4 12.916
74    S3  Waseca 1  R2 134.4 14.811
75    S3  Waseca 1  R3 134.4 12.073
76    S3  Waseca 1  R4 134.4 14.288
77    S3  Waseca 1  R1 168.0 13.453
78    S3  Waseca 1  R2 168.0 13.134
79    S3  Waseca 1  R3 168.0 12.463
80    S3  Waseca 1  R4 168.0 13.242
81    S3  Waseca 1  R1 201.6 13.121
82    S3  Waseca 1  R2 201.6 12.921
83    S3  Waseca 1  R3 201.6 12.261
84    S3  Waseca 1  R4 201.6 13.164
85    S4  Waseca 2  R1   0.0  8.993
86    S4  Waseca 2  R2   0.0  6.183
87    S4  Waseca 2  R3   0.0  7.407
88    S4  Waseca 2  R4   0.0  7.998
89    S4  Waseca 2  R1  44.8 10.018
90    S4  Waseca 2  R2  44.8  9.262
91    S4  Waseca 2  R3  44.8  8.950
92    S4  Waseca 2  R4  44.8  8.368
93    S4  Waseca 2  R1  89.6 12.770
94    S4  Waseca 2  R2  89.6 11.028
95    S4  Waseca 2  R3  89.6 10.385
96    S4  Waseca 2  R4  89.6 11.796
97    S4  Waseca 2  R1 134.4 13.795
98    S4  Waseca 2  R2 134.4 12.100
99    S4  Waseca 2  R3 134.4 12.895
100   S4  Waseca 2  R4 134.4 12.904
101   S4  Waseca 2  R1 179.2 12.004
102   S4  Waseca 2  R2 179.2 14.588
103   S4  Waseca 2  R3 179.2 14.065
104   S4  Waseca 2  R4 179.2 13.639
105   S4  Waseca 2  R1 224.0 11.179
106   S4  Waseca 2  R2 224.0 11.552
107   S4  Waseca 2  R3 224.0 15.221
108   S4  Waseca 2  R4 224.0 13.412
109   S4  Waseca 2  R1 268.8 13.342
110   S4  Waseca 2  R2 268.8 13.232
111   S4  Waseca 2  R3 268.8 14.352
112   S4  Waseca 2  R4 268.8 12.923
113   S5  Olmstead  R1   0.0  8.916
114   S5  Olmstead  R2   0.0  8.177
115   S5  Olmstead  R3   0.0  9.245
116   S5  Olmstead  R4   0.0  8.825
117   S5  Olmstead  R1  33.6 10.981
118   S5  Olmstead  R2  33.6  9.848
119   S5  Olmstead  R3  33.6 10.059
120   S5  Olmstead  R4  33.6  9.810
121   S5  Olmstead  R1  67.2 12.485
122   S5  Olmstead  R2  67.2 10.778
123   S5  Olmstead  R3  67.2 13.425
124   S5  Olmstead  R4  67.2 10.520
125   S5  Olmstead  R1 100.8 14.243
126   S5  Olmstead  R2 100.8 14.395
127   S5  Olmstead  R3 100.8 12.922
128   S5  Olmstead  R4 100.8 13.269
129   S5  Olmstead  R1 134.4 15.426
130   S5  Olmstead  R2 134.4 13.567
131   S5  Olmstead  R3 134.4 15.076
132   S5  Olmstead  R4 134.4 14.175
133   S5  Olmstead  R1 168.0 14.961
134   S5  Olmstead  R2 168.0 14.579
135   S5  Olmstead  R3 168.0 14.409
136   S5  Olmstead  R4 168.0 14.742

# Estadisticas descriptivas
library(psych)
describe(hernandez.nitrogen)
      var   n   mean    sd median trimmed   mad min    max  range  skew
site*   1 136   2.94  1.40   3.00    2.93  1.48 1.0   5.00   4.00  0.04
loc*    2 136   3.00  1.44   3.00    3.00  1.48 1.0   5.00   4.00  0.00
rep*    3 136   2.50  1.12   2.50    2.50  1.48 1.0   4.00   3.00  0.00
nitro   4 136 104.75 73.00 100.80  102.43 99.63 0.0 268.80 268.80  0.21
yield   5 136  11.33  2.39  11.65   11.52  2.39 3.7  15.43  11.73 -0.84
      kurtosis   se
site*    -1.29 0.12
loc*     -1.37 0.12
rep*     -1.38 0.10
nitro    -0.91 6.26
yield     0.71 0.20

# Histograma y curva normal sobreimpuesta 
x <- hernandez.nitrogen$yield
h <- hist(x, breaks = 12, xlab = "Rendimiento", main = "Histogram con curva normal")
xfit <- seq(min(x), max(x), length = 20)
yfit <- dnorm(xfit, mean = mean(x), sd = sd(x))
yfit <- yfit * diff(h$mids[1:2]) * length(x)
lines(xfit, yfit, col = "blue", lwd = 2)


# Histograma y probabilidad
x <- hernandez.nitrogen$yield 
hist(x, xlim=c(3, 18), ylim=c(0, .20), probability=TRUE)
s = sd(x)
m = mean(x)
curve(dnorm(x, mean=m, sd=s), add=TRUE)


# Gráficos Q-Q
qqnorm(hernandez.nitrogen$yield)
qqline(hernandez.nitrogen$yield, col = "red")


# Shapiro-Wilks test
shapiro.test (hernandez.nitrogen$yield)

    Shapiro-Wilk normality test

data:  hernandez.nitrogen$yield
W = 0.9519, p-value = 0.0001108

# Anderson-Darling test
library(nortest)
ad.test(hernandez.nitrogen$yield)

    Anderson-Darling normality test

data:  hernandez.nitrogen$yield
A = 1.398, p-value = 0.001234

# Cramer-von Mises test
cvm.test(hernandez.nitrogen$yield)

    Cramer-von Mises normality test

data:  hernandez.nitrogen$yield
W = 0.2122, p-value = 0.003702

# Kolmogorov-Smirnov test
lillie.test(hernandez.nitrogen$yield)

    Lilliefors (Kolmogorov-Smirnov) normality test

data:  hernandez.nitrogen$yield
D = 0.0869, p-value = 0.0137

# Shapiro-Francia test
sf.test(hernandez.nitrogen$yield)

    Shapiro-Francia normality test

data:  hernandez.nitrogen$yield
W = 0.9531, p-value = 0.0002915

#Jarque-Bera test
library(fBasics) # Necesita timeDate, stabledist, timeSeries, akima
Loading required package: MASS
Loading required package: timeDate
Loading required package: timeSeries
Attaching package: 'fBasics'
The following object is masked from 'package:psych':

tr
The following object is masked from 'package:base':

norm
jbTest(hernandez.nitrogen$yield)

Title:
 Jarque - Bera Normality Test

Test Results:
  PARAMETER:
    Sample Size: 136
  STATISTIC:
    LM: 19.624
    ALM: 21.181
  P VALUE:
    LM p-value: 0.003 
    ALM p-value: 0.004 
    Asymptotic: < 2.2e-16 

Description:
 Tue May 14 14:44:08 2013 by user: Administrator
jarqueberaTest(hernandez.nitrogen$yield)

Title:
 Jarque - Bera Normalality Test

Test Results:
  STATISTIC:
    X-squared: 19.624
  P VALUE:
    Asymptotic p Value: 5.479e-05 

Description:
 Tue May 14 14:44:08 2013 by user: Administrator

#####
# Fuente:
# Datos obtenidos en R Package 'agridat'
# Hernández, J.A. and Mulla, D.J. 2008. Estimating uncertainty of economically optimum fertilizer rates, Agronomy Journal, 100, 1221-1229;

No hay comentarios.: