(By Mr.S.Saravanan & Mr.K.C.Ayyoob) July 2016
Introduction To R Software - https://sites.google.com/view/introductiontorsoftware/
Dear viewers
Feel free to email me about any correction / deletion / addition needed in the details I have given
S.Saravanan
Associate Professor (Computer Science)
PAJANCOA & RI
Karaikal
tssaravananone@gmail.com
http://www.pajancoa.ac.in/staff_aec.htm
For Advanced Doubts Related to Statistics & Statistical Softwares, Students Can Contact
Mr.Ayyoob.K.C, Assistant Professor (Statistics)
Through His Below Email
ayyoobsm@gmail.com
R Terminal Online
https://www.tutorialspoint.com/r_terminal_online.php
Note:
After Seeing $ Prompt, Type R and Press Enter
Other Programming / Coding in Online Through Browsers
https://www.tutorialspoint.com/codingground.htm
R - To open R Console
q(save="no")
R - To get (display) the working directory
R - Example of Assignment Operator = ( <- can also be used instead of = )
R - To list all objects available in the workspace
R - Example of saving a workspace (with all its objects) in a file test.RData
R - Example of loading (open) a workspace (with all its objects) saved in a file test.RData
R - Example of diverting output of R to a file output.txt
R - To cancel the divert enabled through above command
R - Example of Importing dataset from clipboard to a dataframe using read.table()
R - To Create dataframe Using data.frame()
R - Example of Editing a dataframe using fix() function (data editor)
R - Plant Growth Data (dataframe)
R - To convert the above stacked form dataframe to unstacked form
R - To convert the above unstacked form dataframe to stacked form
NewStackedPlantGrowth = stack(UnstackedPlantGrowth)
NewStackedPlantGrowth
values ind
1 4.17 ctrl
2 5.58 ctrl
3 5.18 ctrl
4 6.11 ctrl
5 4.50 ctrl
6 4.61 ctrl
7 5.17 ctrl
8 4.53 ctrl
9 5.33 ctrl
10 5.14 ctrl
11 4.81 trt1
12 4.17 trt1
13 4.41 trt1
14 3.59 trt1
15 5.87 trt1
16 3.83 trt1
17 6.03 trt1
18 4.89 trt1
19 4.32 trt1
20 4.69 trt1
21 6.31 trt2
22 5.12 trt2
23 5.54 trt2
24 5.50 trt2
25 5.37 trt2
26 5.29 trt2
27 4.92 trt2
28 6.15 trt2
29 5.80 trt2
30 5.26 trt2
R - Student's Sleep Data
R - Classical N, P, K Factorial Experiment Data
R - Example of Descriptive Statistics / Summary
R - Example of Sum
R - Example of Arithmetic Mean (Average)
R - Example of Median (Middle Value)
R - Example of Mode (Most Occurred)
R - Example of Range
R - Example of Standard Deviation
R - Example of Variance (Square of Standard Deviation)
- - -
(sd(c(4,5,6,7)))^2
[1] 1.666667
R - Example of Skewness
R - Example of Kurtosis
R - Syntax, Example for Simple Linear Regression (lm - Linear Model)
R - Syntax, Example for Multiple Linear Regression (lm - Linear Model)
R - Example of Covariance
R - Example of Correlation coefficient
R - Example of (Welch) (Two Samples) Unpaired (Independent) (Student's) t-test Assuming Unequal Variances
Welch Two Sample t-test
data: sleep$extra by sleep$group
t = -1.8608, df = 17.776, p-value = 0.07939
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-3.3654832 0.2054832
sample estimates:
mean in group 1 mean in group 2
0.75 2.33
R - Example of (Two Samples) Unpaired (Independent) (Student's) t-test Assuming Equal Variances
t.test(sleep$extra ~ sleep$group, var.equal=TRUE)
Two Sample t-test
data: sleep$extra by sleep$group
t = -1.8608, df = 18, p-value = 0.07919
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-3.363874 0.203874
sample estimates:
mean in group 1 mean in group 2
0.75 2.33
R - Example of (Two Samples) Paired (Student's) t-test
Paired t-test
data: sleep$extra by sleep$group
t = -4.0621, df = 9, p-value = 0.002833
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
-2.4598858 -0.7001142
sample estimates:
mean of the differences
-1.58
R - Example of z-test
Two-sample z-Test
data: x and y
z = 7.5568, p-value = 4.13e-14
alternative hypothesis: true difference in means is not equal to 0
95 percent confidence interval:
1.300323 2.211040
sample estimates:
mean of x mean of y
7.018182 5.262500
Note:
null hypothesis: true difference in means is equal to 0
If y = NULL, a one-sample z-test is carried out with ‘x’
R - Examples For Independent Variable(s) / Treatments (Whose Effects on Dependent Variable We Compare)
R - Example For Dependent Variable / Response
R - Example of Completely Randomized Design ANOVA (One-way ANOVA)
mango = data.frame(
yield=c(34, 45, 36, 54, 55, 45, 37, 33, 56),
variety=c("var1", "var1", "var1", "var2", "var2", "var2", "var3", "var3", "var3") )
mango
yield variety
1 34 var1
2 45 var1
3 36 var1
4 54 var2
5 55 var2
6 45 var2
7 37 var3
8 33 var3
9 56 var3
summary(aov(yield~variety, data=mango))
Df Sum Sq Mean Sq F value Pr(>F)
variety 2 269.6 134.78 1.875 0.233
Residuals 6 431.3 71.89
R - Example For Completely Randomized Design (CRD)
library(agricolae)
treatments=c("trt1", "trt2", "trt3", "trt4")
reps = 3
result = design.crd(treatments, reps, randomization=FALSE)
result
$parameters
$parameters$design
[1] "crd"
$parameters$trt
[1] "trt1" "trt2" "trt3" "trt4"
$parameters$r
[1] 3 3 3 3
$parameters$serie
[1] 2
$parameters$seed
[1] 946090695
$parameters$kinds
[1] "Super-Duper"
$parameters[[7]]
[1] FALSE
$book
plots r treatments
1 101 1 trt1
2 102 2 trt1
3 103 3 trt1
4 104 1 trt2
5 105 2 trt2
6 106 3 trt2
7 107 1 trt3
8 108 2 trt3
9 109 3 trt3
10 110 1 trt4
11 111 2 trt4
12 112 3 trt4
R - Example of Randomized (Complete) Block Design ANOVA
(Two-way ANOVA Without Interaction Between 2 Factors)
Note:
<independent variable2> is the blocking factor
In RBD, each block have all the treatments.
In CRD, replications can occur in any part of the field and each treatment can have different number of replications.
lemon = data.frame(
yield = c(34, 45, 36, 38, 54, 55, 45, 48, 37, 33, 56, 55),
treatments = c("trt1", "trt1", "trt1", "trt1", "trt2", "trt2", "trt2", "trt2", "trt3", "trt3", "trt3", "trt3"),
repsblocks = c("blk1", "blk2", "blk3", "blk4", "blk1", "blk2", "blk3", "blk4", "blk1", "blk2", "blk3", "blk4") )
lemon
yield treatments repsblocks
1 34 trt1 blk1
2 45 trt1 blk2
3 36 trt1 blk3
4 38 trt1 blk4
5 54 trt2 blk1
6 55 trt2 blk2
7 45 trt2 blk3
8 48 trt2 blk4
9 37 trt3 blk1
10 33 trt3 blk2
11 56 trt3 blk3
12 55 trt3 blk4
summary(aov(yield~treatments+repsblocks, data=lemon))
Df Sum Sq Mean Sq F value Pr(>F)
treatments 2 302.2 151.08 1.744 0.253
repsblocks 3 46.7 15.56 0.180 0.906
Residuals 6 519.8 86.64
R - Example For Randomized (Complete) Block Design (RCBD / RBD)
library(agricolae)
treatments=c("trt1", "trt2", "trt3", "trt4")
repsblocks = 3
result = design.rcbd(treatments, repsblocks, randomization=FALSE)
result
$parameters
$parameters$design
[1] "rcbd"
$parameters$trt
[1] "trt1" "trt2" "trt3" "trt4"
$parameters$r
[1] 3
$parameters$serie
[1] 2
$parameters$seed
[1] -434065305
$parameters$kinds
[1] "Super-Duper"
$parameters[[7]]
[1] FALSE
$sketch
[,1] [,2] [,3] [,4]
[1,] "trt1" "trt2" "trt3" "trt4"
[2,] "trt1" "trt2" "trt3" "trt4"
[3,] "trt1" "trt2" "trt3" "trt4"
$book
plots block treatments
1 101 1 trt1
2 102 1 trt2
3 103 1 trt3
4 104 1 trt4
5 201 2 trt1
6 202 2 trt2
7 203 2 trt3
8 204 2 trt4
9 301 3 trt1
10 302 3 trt2
11 303 3 trt3
12 304 3 trt4
R - Example of ANOVA with Interactions between N, P & K
Df Sum Sq Mean Sq F value Pr(>F)
block 5 343.3 68.66 4.447 0.01594 *
N 1 189.3 189.28 12.259 0.00437 **
P 1 8.4 8.40 0.544 0.47490
K 1 95.2 95.20 6.166 0.02880 *
N:P 1 21.3 21.28 1.378 0.26317
N:K 1 33.1 33.13 2.146 0.16865
P:K 1 0.5 0.48 0.031 0.86275
Residuals 12 185.3 15.44
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
R - Example of ANOVA without any Interactions between N, P & K
Df Sum Sq Mean Sq F value Pr(>F)
block 5 343.3 68.66 4.288 0.01272 *
N 1 189.3 189.28 11.821 0.00366 **
P 1 8.4 8.40 0.525 0.47999
K 1 95.2 95.20 5.946 0.02767 *
Residuals 15 240.2 16.01
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
R - Example For Latin Square Design (LSD)
varieties = c("malgova", "banganapalli", "alphonsa", "senthura", "kalapadi")
result = design.lsd(varieties)
result
$parameters
$parameters$design
[1] "lsd"
$parameters$trt
[1] "malgova" "banganapalli" "alphonsa" "senthura" "kalapadi"
$parameters$r
[1] 5
$parameters$serie
[1] 2
$parameters$seed
[1] 1527168179
$parameters$kinds
[1] "Super-Duper"
$parameters[[7]]
[1] TRUE
$sketch
[,1] [,2] [,3] [,4] [,5]
[1,] "senthura" "alphonsa" "malgova" "banganapalli" "kalapadi"
[2,] "alphonsa" "banganapalli" "kalapadi" "malgova" "senthura"
[3,] "kalapadi" "senthura" "banganapalli" "alphonsa" "malgova"
[4,] "malgova" "kalapadi" "alphonsa" "senthura" "banganapalli"
[5,] "banganapalli" "malgova" "senthura" "kalapadi" "alphonsa"
$book
plots row col varieties
1 101 1 1 senthura
2 102 1 2 alphonsa
3 103 1 3 malgova
4 104 1 4 banganapalli
5 105 1 5 kalapadi
6 201 2 1 alphonsa
7 202 2 2 banganapalli
8 203 2 3 kalapadi
9 204 2 4 malgova
10 205 2 5 senthura
11 301 3 1 kalapadi
12 302 3 2 senthura
13 303 3 3 banganapalli
14 304 3 4 alphonsa
15 305 3 5 malgova
16 401 4 1 malgova
17 402 4 2 kalapadi
18 403 4 3 alphonsa
19 404 4 4 senthura
20 405 4 5 banganapalli
21 501 5 1 banganapalli
22 502 5 2 malgova
23 503 5 3 senthura
24 504 5 4 kalapadi
25 505 5 5 alphonsa
R - Example For Split Plot Design
trtinplots = c("trt1", "trt2", "trt3")
trtinsubplots = c("trtsub1", "trtsub2", "trtsub3", "trtsub4")
repsblocks = 2
result = design.split(trtinplots, trtinsubplots, repsblocks, randomization = FALSE)
result
$parameters
$parameters$design
[1] "split"
$parameters[[2]]
[1] TRUE
$parameters$trt1
[1] "trt1" "trt2" "trt3"
$parameters$applied
[1] "rcbd"
$parameters$r
[1] 2
$parameters$serie
[1] 2
$parameters$seed
[1] -1781763121
$parameters$kinds
[1] "Super-Duper"
$book
plots splots block trtinplots trtinsubplots
1 101 1 1 trt2 trtsub1
2 101 2 1 trt2 trtsub2
3 101 3 1 trt2 trtsub3
4 101 4 1 trt2 trtsub4
5 102 1 1 trt3 trtsub1
6 102 2 1 trt3 trtsub2
7 102 3 1 trt3 trtsub3
8 102 4 1 trt3 trtsub4
9 103 1 1 trt1 trtsub1
10 103 2 1 trt1 trtsub2
11 103 3 1 trt1 trtsub3
12 103 4 1 trt1 trtsub4
13 201 1 2 trt2 trtsub1
14 201 2 2 trt2 trtsub2
15 201 3 2 trt2 trtsub3
16 201 4 2 trt2 trtsub4
17 202 1 2 trt1 trtsub1
18 202 2 2 trt1 trtsub2
19 202 3 2 trt1 trtsub3
20 202 4 2 trt1 trtsub4
21 203 1 2 trt3 trtsub1
22 203 2 2 trt3 trtsub2
23 203 3 2 trt3 trtsub3
24 203 4 2 trt3 trtsub4
R - Example For Split Plot Design ANOVA
Syntax
sp.plot(repsblocks, mainplotfactor, subplotfactor, response)
Example
library("agricolae")
data(plots,package="agricolae")
plots
block plot A B yield
1 1 p1 a1 b1 4
2 1 p1 a1 b2 1
3 1 p1 a1 b3 9
4 1 p2 a2 b1 6
5 1 p2 a2 b2 10
6 1 p2 a2 b3 2
7 2 p3 a1 b1 5
8 2 p3 a1 b2 3
9 2 p3 a1 b3 10
10 2 p4 a2 b1 4
11 2 p4 a2 b2 14
12 2 p4 a2 b3 1
13 3 p5 a1 b1 2
14 3 p5 a1 b2 2
15 3 p5 a1 b3 15
16 3 p6 a2 b1 3
17 3 p6 a2 b2 12
18 3 p6 a2 b3 1
sp.plot(plots$block, plots$A, plots$B, plots$yield)
ANALYSIS SPLIT PLOT: plots$yield
Class level information
plots$A : a1 a2
plots$B : b1 b2 b3
plots$block : 1 2 3
Number of observations: 18
Analysis of Variance Table
Response: plots$yield
Df Sum Sq Mean Sq F value Pr(>F)
plots$block 2 2.111 1.056 0.5135 0.6607143
plots$A 1 0.222 0.222 0.1081 0.7735446
Ea 2 4.111 2.056
plots$B 2 29.778 14.889 3.4581 0.0827438 .
plots$A:plots$B 2 300.444 150.222 34.8903 0.0001119 ***
Eb 8 34.444 4.306
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
cv(a) = 24.8 %, cv(b) = 35.9 %, Mean = 5.777778
R - Example For Split Split Plot Design ANOVA
Syntax
ssp.plot(repsblocks, mainplotfactor, subplotfactor, subsubplotfactor, response)
Note: Split Split Plot Design is an Extension of the Split Plot Design to Accommodate a Third Factor
Example
library("agricolae")
yield<-c(3.320,3.766,4.660,3.188,3.625,5.232,5.468,5.759,6.215,
4.246,5.255,6.829,3.132,5.389,5.217,3.864,4.311,5.915,4.752,
4.809,5.170,5.788,6.130,7.106,4.842,5.742,5.869,4.375,4.315,
5.389,4.507,4.875,5.400,4.756,5.295,6.046,4.422,5.308,6.318,
4.863,5.345,6.011,4.678,5.896,7.309,6.101,5.096,6.573,5.595,
6.357,7.016,5.442,6.398,6.953,6.209,6.992,7.565,6.860,6.857,
7.254,5.122,4.873,5.495,6.780,5.925,7.442,5.988,6.533,6.914,
6.768,7.856,7.626,6.894,6.974,7.812,4.815,4.166,4.225,5.390,
5.163,4.478,6.509,6.569,7.991,5.779,6.164,7.362,6.573,7.422,
8.950,5.355,7.442,7.018,6.706,8.592,8.480,8.452,8.662,9.112,
8.042,9.080,9.660,9.314,9.224,10.360,5.536,6.462,8.020,6.546,
7.646,9.942,6.698,8.526,9.140,7.414,9.016,8.966,8.508,9.680,
9.896,5.244,5.584,7.642,7.092,7.212,8.714,8.650,8.514,9.320,
6.902,7.778,9.128,8.032,9.294,9.712)
repsblocks<-rep(gl(3,15),3)
nitrogen<-rep(c(rep(0,3),rep(50,3),rep(80,3),rep(110,3),rep(140,3)),9)
management<-rep(c("m1","m2","m3"),45)
variety<-gl(3,45)
yield
[1] 3.320 3.766 4.660 3.188 3.625 5.232 5.468 5.759 6.215 4.246
[11] 5.255 6.829 3.132 5.389 5.217 3.864 4.311 5.915 4.752 4.809
[21] 5.170 5.788 6.130 7.106 4.842 5.742 5.869 4.375 4.315 5.389
[31] 4.507 4.875 5.400 4.756 5.295 6.046 4.422 5.308 6.318 4.863
[41] 5.345 6.011 4.678 5.896 7.309 6.101 5.096 6.573 5.595 6.357
[51] 7.016 5.442 6.398 6.953 6.209 6.992 7.565 6.860 6.857 7.254
[61] 5.122 4.873 5.495 6.780 5.925 7.442 5.988 6.533 6.914 6.768
[71] 7.856 7.626 6.894 6.974 7.812 4.815 4.166 4.225 5.390 5.163
[81] 4.478 6.509 6.569 7.991 5.779 6.164 7.362 6.573 7.422 8.950
[91] 5.355 7.442 7.018 6.706 8.592 8.480 8.452 8.662 9.112 8.042
[101] 9.080 9.660 9.314 9.224 10.360 5.536 6.462 8.020 6.546 7.646
[111] 9.942 6.698 8.526 9.140 7.414 9.016 8.966 8.508 9.680 9.896
[121] 5.244 5.584 7.642 7.092 7.212 8.714 8.650 8.514 9.320 6.902
[131] 7.778 9.128 8.032 9.294 9.712
repsblocks
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3
[38] 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[75] 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2
[112] 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
Levels: 1 2 3
nitrogen
[1] 0 0 0 50 50 50 80 80 80 110 110 110 140 140 140 0 0 0
[19] 50 50 50 80 80 80 110 110 110 140 140 140 0 0 0 50 50 50
[37] 80 80 80 110 110 110 140 140 140 0 0 0 50 50 50 80 80 80
[55] 110 110 110 140 140 140 0 0 0 50 50 50 80 80 80 110 110 110
[73] 140 140 140 0 0 0 50 50 50 80 80 80 110 110 110 140 140 140
[91] 0 0 0 50 50 50 80 80 80 110 110 110 140 140 140 0 0 0
[109] 50 50 50 80 80 80 110 110 110 140 140 140 0 0 0 50 50 50
[127] 80 80 80 110 110 110 140 140 140
management
[1] "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3"
[16] "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3"
[31] "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3"
[46] "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3"
[61] "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3"
[76] "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3"
[91] "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3"
[106] "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3"
[121] "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3" "m1" "m2" "m3"
variety
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[38] 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[75] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
[112] 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
Levels: 1 2 3
ssp.plot(repsblocks,nitrogen,management,variety,yield)
ANALYSIS SPLIT-SPLIT PLOT: yield
Class level information
nitrogen : 0 50 80 110 140
management : m1 m2 m3
variety : 1 2 3
repsblocks : 1 2 3
Number of observations: 135
Analysis of Variance Table
Response: yield
Df Sum Sq Mean Sq F value Pr(>F)
repsblocks 2 0.732 0.366 0.6578 0.543910
nitrogen 4 61.641 15.410 27.6953 9.734e-05 ***
Ea 8 4.451 0.556
management 2 42.936 21.468 81.9965 2.303e-10 ***
nitrogen:management 8 1.103 0.138 0.5266 0.822648
Eb 20 5.236 0.262
variety 2 206.013 103.007 207.8667 < 2.2e-16 ***
variety:nitrogen 8 14.145 1.768 3.5679 0.001916 **
variety:management 4 3.852 0.963 1.9432 0.114899
variety:nitrogen:management 16 3.699 0.231 0.4666 0.953759
Ec 60 29.732 0.496
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
cv(a) = 11.4 %, cv(b) = 7.8 %, cv(c) = 10.7 %, Mean = 6.554415
R - Example For Strip Plot (Split Block) Design
rowtrts = c("rtrt1", "rtrt2", "rtrt3", "rtrt4")
coltrts = c("ctrt1", "ctrt2", "ctrt3")
repsblocks = 2
result = design.strip(rowtrts, coltrts, repsblocks, randomization = FALSE)
result
$parameters
$parameters$design
[1] "strip"
$parameters$trt1
[1] "rtrt1" "rtrt2" "rtrt3" "rtrt4"
$parameters$trt2
[1] "ctrt1" "ctrt2" "ctrt3"
$parameters$r
[1] 2
$parameters$serie
[1] 2
$parameters$seed
[1] -1745342809
$parameters$kinds
[1] "Super-Duper"
$book
plots block rowtrts coltrts
1 101 1 rtrt1 ctrt1
2 102 1 rtrt1 ctrt2
3 103 1 rtrt1 ctrt3
4 104 1 rtrt2 ctrt1
5 105 1 rtrt2 ctrt2
6 106 1 rtrt2 ctrt3
7 107 1 rtrt3 ctrt1
8 108 1 rtrt3 ctrt2
9 109 1 rtrt3 ctrt3
10 110 1 rtrt4 ctrt1
11 111 1 rtrt4 ctrt2
12 112 1 rtrt4 ctrt3
13 201 2 rtrt1 ctrt1
14 202 2 rtrt1 ctrt2
15 203 2 rtrt1 ctrt3
16 204 2 rtrt2 ctrt1
17 205 2 rtrt2 ctrt2
18 206 2 rtrt2 ctrt3
19 207 2 rtrt3 ctrt1
20 208 2 rtrt3 ctrt2
21 209 2 rtrt3 ctrt3
22 210 2 rtrt4 ctrt1
23 211 2 rtrt4 ctrt2
24 212 2 rtrt4 ctrt3
R - Example For Strip Plot (Split Block) Design ANOVA
Syntax
strip.plot(repsblocks, columnfactor, rowfactor, response)
Note: Strip Plot Design ANOVA is suitable for measuring the interaction effects between column factor & row factor
Example
library("agricolae")
data(huasahuasi, package="agricolae")
huasahuasi$YIELD
block trt clon y1da y2da y3da
1 I 40mm C387164.4 22.75 5.50 4.14
2 I 40mm C386209.10 10.80 8.00 6.10
3 I 40mm Cruza148 1.60 11.70 4.15
4 I 40mm Yungay 10.92 9.90 5.20
5 I 40mm Musuq 3.50 5.10 2.40
6 I 7-days C387164.4 21.98 7.00 7.60
7 I 7-days C386209.10 10.50 12.50 3.60
8 I 7-days Cruza148 2.80 9.15 4.90
9 I 7-days Yungay 18.00 8.80 5.90
10 I 7-days Musuq 2.55 4.60 3.50
11 I Non-application C387164.4 29.30 2.50 6.50
12 I Non-application C386209.10 10.75 2.85 2.50
13 I Non-application Cruza148 10.50 6.30 4.20
14 I Non-application Yungay 8.45 6.60 5.60
15 I Non-application Musuq 0.00 0.80 1.65
16 II Non-application Musuq 0.00 0.00 0.40
17 II Non-application Yungay 5.50 6.10 2.15
18 II Non-application C386209.10 5.40 4.10 2.70
19 II Non-application Cruza148 6.30 7.10 5.80
20 II Non-application C387164.4 30.30 5.20 1.60
21 II 40mm Musuq 0.00 0.85 1.30
22 II 40mm Yungay 14.10 10.70 1.95
23 II 40mm C386209.10 12.00 3.80 3.10
24 II 40mm Cruza148 6.40 6.00 5.70
25 II 40mm C387164.4 46.60 4.10 2.40
26 II 7-days Musuq 6.00 3.60 2.30
27 II 7-days Yungay 14.30 5.90 2.70
28 II 7-days C386209.10 15.65 4.90 6.30
29 II 7-days Cruza148 5.00 7.30 8.00
30 II 7-days C387164.4 32.10 5.20 2.00
31 III 7-days C386209.10 10.40 8.35 3.05
32 III 7-days Musuq 0.70 2.70 3.70
33 III 7-days C387164.4 32.10 5.70 3.10
34 III 7-days Yungay 25.90 10.00 3.20
35 III 7-days Cruza148 9.30 8.60 3.55
36 III Non-application C386209.10 4.85 6.50 4.40
37 III Non-application Musuq 0.00 0.25 0.70
38 III Non-application C387164.4 29.20 5.50 4.65
39 III Non-application Yungay 13.70 12.10 1.10
40 III Non-application Cruza148 3.80 10.90 4.70
41 III 40mm C386209.10 5.10 5.85 3.20
42 III 40mm Musuq 0.50 1.00 1.10
43 III 40mm C387164.4 29.30 3.80 4.90
44 III 40mm Yungay 15.70 8.30 1.40
45 III 40mm Cruza148 8.10 10.60 5.80
yieldresponse = huasahuasi$YIELD$y1da + huasahuasi$YIELD$y2da + huasahuasi$YIELD$y3da
repsblocks = huasahuasi$YIELD$block
columnfactor = huasahuasi$YIELD$clon
rowfactor = huasahuasi$YIELD$trt
strip.plot(repsblocks, columnfactor, rowfactor, yieldresponse)
ANALYSIS STRIP PLOT: yieldresponse
Class level information
columnfactor : C387164.4 C386209.10 Cruza148 Yungay Musuq
rowfactor : 40mm 7-days Non-application
repsblocks : I II III
Number of observations: 45
model Y: yieldresponse ~ repsblocks + columnfactor + Ea + rowfactor + Eb + rowfactor:columnfactor + Ec
Analysis of Variance Table
Response: yieldresponse
Df Sum Sq Mean Sq F value Pr(>F)
repsblocks 2 7.5 3.75 0.2139 0.80970
columnfactor 4 5435.8 1358.96 36.2754 3.572e-05 ***
Ea 8 299.7 37.46 2.1343 0.09368 .
rowfactor 2 280.6 140.32 8.5361 0.03603 *
Eb 4 65.8 16.44 0.9365 0.46785
rowfactor:columnfactor 8 194.1 24.26 1.3821 0.27622
Ec 16 280.8 17.55
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
cv(a) = 27.7 %, cv(b) = 18.4 %, cv(c) = 19 %, Mean = 22.08644
R - Transformation - Examples For Absolute Value
abs(-16)
[1] 16
abs(16)
[1] 16
R - Transformation - Examples For Square Root
sqrt(16)
[1] 4
R - Transformation - Examples For Common Logarithm - Logarithm to Base 10
log10(1)
[1] 0
log10(10)
[1] 1
log10(100)
[1] 2
log10(1000)
[1] 3
log10(10000)
[1] 4
Note: Logarithmic scales reduce wide ranging quantities to tiny scopes
R - Transformation - Examples For Binary Logarithm - Logarithm to Base 2
log2(1)
[1] 0
log2(10)
[1] 3.321928
log2(100)
[1] 6.643856
log2(1000)
[1] 9.965784
log2(10000)
[1] 13.28771
R - Transformation - Examples For Natural Logarithm - Logarithm to Base e (e = exp(1) ~ 2.71828)
e = Euler's number = Napier's constant
log(10)
[1] 2.302585
log(100)
[1] 4.60517
log(1000)
[1] 6.907755
log(10000)
[1] 9.21034
log(1)
[1] 0
R - Transformation - Examples For Exponential Function
(Inverse of exponential function is natural logarithmic function)
exp(2.302585)
[1] 9.999999
exp(4.60517)
[1] 99.99998
exp(6.907755)
[1] 999.9997
exp(9.21034)
[1] 9999.996
exp(0)
[1] 1
R - Transformation - Example For Logit & Inverse Logit
library("gtools")
dataforlogit=.1*1:9
dataforlogit
[1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
logitresult=logit(dataforlogit)
logitresult
[1] -2.1972246 -1.3862944 -0.8472979 -0.4054651 0.0000000 0.4054651 0.8472979
[8] 1.3862944 2.1972246
inv.logit(logitresult)
[1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9
R - Transformation - Examples For Arc-Sine
asin(0)
[1] 0
asin(0.1)
[1] 0.1001674
asin(0.2)
[1] 0.2013579
asin(0.3)
[1] 0.3046927
asin(0.4)
[1] 0.4115168
asin(0.5)
[1] 0.5235988
asin(0.6)
[1] 0.6435011
asin(0.7)
[1] 0.7753975
asin(0.8)
[1] 0.9272952
asin(0.9)
[1] 1.11977
asin(1)
[1] 1.570796
R - Transformation - Examples For Sine
R - Example For Chi-square Goodness of Fit Test
library("stats")
R - Example For Chi-squared Contingency Table Tests (Chi-square Test of Independence)
library("stats")
R - Example For Simple Plot Using Dots
plot(PlantGrowth$weight)
R - Example For Simple Plot Using Lines
plot(PlantGrowth$weight, type=”l”)
R - Example For Simple Plot Using Both Lines & Dots
plot(PlantGrowth$weight, type=”b”)
R - Example For Simple Plot Using Vertical Lines
plot(PlantGrowth$weight, type=”h”)
R - Example For Simple Plot Using Steps
plot(PlantGrowth$weight, type=”s”)
R - Example For Scatter Plot Using Dots
plot(age~circumference, Orange)
Note: 1st Variable in Vertical Axes & 2nd Variable in Horizontal Axes
R - Example For Scatter Plot Using Lines
plot(age~circumference, Orange, type=”l”))
Note: 1st Variable in Vertical Axes & 2nd Variable in Horizontal Axes
R - Example For Scatter Plot Using Both Dots & Lines
plot(age~circumference, Orange, type=”b”))
Note: 1st Variable in Vertical Axes & 2nd Variable in Horizontal Axes
R - Example For Histogram
hist(PlantGrowth$weight)
R - Example For Pie Plot
pie(table(Orange$age))
R - Example For Clustered Vertical Bar Plot / Column Chart
Population = data.frame(
row.names=c("Year1970", "Year1980", "Year1990", "Year2000", "Year2010"),
India=c(45, 47, 40, 34, 33),
Japan=c(38, 34, 38, 28, 29),
US=c(32, 31, 28, 47, 46),
UK=c(27, 26, 25, 36, 38),
China=c(30, 33, 24, 37, 32))
Population
India Japan US UK China
Year1970 45 38 32 27 30
Year1980 47 34 31 26 33
Year1990 40 38 28 25 24
Year2000 34 28 47 36 37
Year2010 33 29 46 38 32
barplot(as.matrix(Population),beside=T,legend=row.names(Population))
R - Examples of Saving the Plot in the Active Device Window as Image
savePlot("graph1.png", type="png")
savePlot("graph1.jpg", type="jpeg")
savePlot("graph1.tif", type="tiff")
R - Example For Least Significant Difference (LSD) Test / Critical Difference (CD)
p (p-value) = Probability
alpha = Threshold Value For p = Significance Level of the Test - Often Set to 0.05 (5%) or 0.01 (1%) or 0.1 (10%)
H0 - Null Hypothesis - Assumes No (Null) Relationship Exists Among Groups in the Population
---
In ANOVA (Analysis of Variance)
If ( p < alpha )
Significant
Significantly Different
Null Hypothesis Rejected
Relationship Exists Among Groups in the Population
Proceed With LSD For Finding Which Group Having Relationship With Which Other Group(s) in the Population
Else
Non Significant
Groups Are Not Significantly Different
No (Null) Relationship Exists Among Groups in the Population
Null Hypothesis Accepted
---
Syntax
LSD.test(Response, Treatments, Residuals Degrees of Freedom From ANOVA, Residuals Mean Square From ANOVA, alpha = 0.05)
- - -
Density of 5 Ponds
density ponds
28.2 pond1
33.2 pond1
36.4 pond1
34.6 pond1
29.1 pond1
31 pond1
39.6 pond2
40.8 pond2
37.9 pond2
37.1 pond2
43.6 pond2
42.4 pond2
46.3 pond3
42.1 pond3
43.5 pond3
48.8 pond3
43.7 pond3
40.1 pond3
41 pond4
44.1 pond4
46.4 pond4
40.2 pond4
38.6 pond4
36.3 pond4
56.3 pond5
54.1 pond5
59.4 pond5
62.7 pond5
60 pond5
57.3 pond5
---
Select the above data kept in LibreOffice Calc > Right click over the selection > Context menu appears > Select Copy
water = read.table("clipboard",header=T)
summary(aov(density~ponds, data=water))
Df Sum Sq Mean Sq F value Pr(>F)
ponds 4 2193.4 548.4 56.16 3.95e-12 ***
Residuals 25 244.1 9.8
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
library("agricolae")
result = LSD.test(water$density, water$ponds, 25, 9.8, alpha = 0.05)
result
$statistics
Mean CV MSerror LSD
43.16 7.253233 9.8 3.722394
$parameters
Df ntr t.value alpha test name.t
25 5 2.059539 0.05 Fisher-LSD water$ponds
$means
water$density std r LCL UCL Min Max
pond1 32.08333 3.205256 6 29.45120 34.71546 28.2 36.4
pond2 40.23333 2.530349 6 37.60120 42.86546 37.1 43.6
pond3 44.08333 3.080530 6 41.45120 46.71546 40.1 48.8
pond4 41.10000 3.666061 6 38.46787 43.73213 36.3 46.4
pond5 58.30000 3.036445 6 55.66787 60.93213 54.1 62.7
$comparison
NULL
$groups
trt means M
1 pond5 58.30000 a
2 pond3 44.08333 b
3 pond4 41.10000 bc
4 pond2 40.23333 c
5 pond1 32.08333 d
R - To get help in browser instead of the in-build R (in Ubuntu 14.04.1)
Note:
texinfo - needed to avoid error "/usr/bin/texi2dvi: not found" while giving help like "?sink" in R, after command "options(help_type = "pdf")"
Even after installing above texinfo, gives error "Error in texi2dvi(file = file, pdf = TRUE", in Ubuntu 14.04.1
which may be related to error "Dates is specific format in header settings prevent rendering to PDF"
If No Proxy, then it should also have 127.0.0.1 in No Proxy for html to work
R - Example of Running system / terminal commands like "clear" inside R
R - To Install Additional Package (By Selection) Through Internet
R - Example For Installing a Particular Additional Package Through Internet
R - To Install Additional Package Without Internet
(.tar.gz Package Files Are Kept Locally in $HOME/packages/ Along With Dependecny Package Files)
R - To Display R Version, Platform, Attached, Loaded (and Not Attached) Packages
R - To List All Loaded Packages
R - To List All Loaded & Attached Packages (To List Packages Included in The Search Path)
R - To List All Installed Packages
.packages(all.available = TRUE)
R - Example - To Load and Attach an Installed Package
R - Example - To Detach & Try To Unload a Loaded & Attached Package
R - To List Available Datasets, Excluding Datasets From Unloaded Pacakges
R - To List Available Datasets, Including Datasets From Unloaded Packages
R - Example of Listing All Datasets Available in a Package (Even if Not Loaded)
R - Example of Loading Specific Dataset From a Specific Package
R - Example of Getting Information About a Dataset
R - Example For Creating / Testing Time Series Objects
R - Example For AutoRegressive (AR) Modelling of Time Series & Predicting
$pred
Time Series:
Start = 1989
End = 2013
Frequency = 1
[1] 135.25933 148.09051 133.98476 106.61344 71.21921 40.84057 18.70100
[8] 11.52416 27.24208 56.99888 87.86705 107.62926 111.05437 98.05484
[15] 74.84085 48.80128 27.65441 18.15075 23.15355 40.04723 61.95906
[22] 80.79092 90.11420 87.44131 74.42284
$se
Time Series:
Start = 1989
End = 2013
Frequency = 1
[1] 16.35519 24.68467 28.95653 29.97401 30.07714 30.15629 30.35971 30.58793
[9] 30.71100 30.74276 31.42565 32.96467 34.48910 35.33601 35.51890 35.52034
[17] 35.65505 35.90628 36.07084 36.08139 36.16818 36.56324 37.16527 37.64820
[25] 37.83954
Note: $pred - Prediction, $se - Standard Error
R - Example For Moving Average
Syntax
SMA(x, n)
x: Price, Volume, etc... Data Series
n: Number of Periods To Average Over
Note: SMA - Simple Moving Average Calculates the Average of the Series Over the Past n Observations
Example
library("TTR")
data("ttrc")
x = ttrc$Close[1:50]
x
[1] 3.08 3.11 3.09 3.10 3.11 3.16 3.22 3.23 3.32 3.30 3.32 3.26 3.16 3.25 3.29
[16] 3.31 3.32 3.37 3.42 3.42 3.44 3.41 3.40 3.43 3.46 3.39 3.43 3.47 3.48 3.50
[31] 3.58 3.54 3.51 3.49 3.48 3.46 3.46 3.45 3.50 3.49 3.51 3.55 3.48 3.49 3.44
[46] 3.40 3.40 3.39 3.42 3.37
SMA(x, 20)
[1] NA NA NA NA NA NA NA NA NA NA
[11] NA NA NA NA NA NA NA NA NA 3.2420
[21] 3.2600 3.2750 3.2905 3.3070 3.3245 3.3360 3.3465 3.3585 3.3665 3.3765
[31] 3.3895 3.4035 3.4210 3.4330 3.4425 3.4500 3.4570 3.4610 3.4650 3.4685
[41] 3.4720 3.4790 3.4830 3.4860 3.4850 3.4855 3.4840 3.4800 3.4770 3.4705
mean(x[1:20])
[1] 3.242
mean(x[2:21])
[1] 3.26
R - Example For ARIMA (AutoRegressive Integrated Moving Average) Modelling of Time Series & Predicting
Call:
arima(x = lh, order = c(3, 0, 0))
Coefficients:
ar1 ar2 ar3 intercept
0.6448 -0.0634 -0.2198 2.3931
s.e. 0.1394 0.1668 0.1421 0.0963
sigma^2 estimated as 0.1787: log likelihood = -27.09, aic = 64.18
predict(arimalh, n.ahead = 12)
$pred
Time Series:
Start = 49
End = 60
Frequency = 1
[1] 2.460181 2.270846 2.198618 2.260719 2.346956 2.414502 2.438940 2.431461
[9] 2.410244 2.391665 2.382674 2.382717
$se
Time Series:
Start = 49
End = 60
Frequency = 1
[1] 0.4226823 0.5029319 0.5245248 0.5247153 0.5305493 0.5369155 0.5388042
[8] 0.5388446 0.5391041 0.5395172 0.5396989 0.5397138
Simple HTML Example ( simple.html )
<!DOCTYPE html>
<html>
<head>
<title> Title </title>
</head>
<body>
<p> Paragraph Content </p>
</body>
</html>
<html>
<head>
<title> Title </title>
</head>
<body>
<p> Paragraph - Normal Size </p>
<h3> Heading Level 3 - Bold and Big Size </h3>
<h2> Heading Level 2 - Bold and Bigger </h2>
<h1> Heading Level 1 - Bold and Biggest </h1>
<b> Bold </b>
<i> Italic </i>
<u> Underline </u>
<table border=1>
<tr>
<th> Table Heading Data 1 </th>
<th> Table Heading Data 2 </th>
</tr>
<tr>
<td> Table Data 1 in Row 2 </td>
<td> Table Data 2 in Row 2 </td>
</tr>
<tr>
<td> Table Data 1 in Row 3 </td>
<td> Table Data 2 in Row 3 </td>
</tr>
</table>
</body>
</html>
<html>
<head>
<meta name="description" content="Students Syllabus">
</head>
<body>
<img src="pp.jpg">
<hr>
<p> paragraph 1 </p>
<hr>
<br>
<p> This paragraph has a break above and below </ p>
<br>
<hr>
</body>
</html>
Note:
<link rel="stylesheet" href="mystyle.css" type="text/css">
explained in next paragraph also is a void HTML Element
<html>
<body>
<p style="color:blue;text-align:center">Paragraph with inline style</p>
</body>
</html>
<html>
<head>
<style>
mystyle {
background-color: rgb(255,0,0);
font-size: 30px;
color: rgb(0,255,0);
}
</style>
</head>
<body>
<mystyle>Style applied from internal style</mystyle>
</body>
</html>
<html>
<head>
<link rel="stylesheet" href="extstyle.css" type="text/css">
</head>
<body>
<mystyle>Style applied from external style sheet file</mystyle>
</body>
</html>
Note: Below content alone should be inside the external style sheet file ( extstyle.css )
mystyle {
background-color: rgb(255,0,0);
font-size: 30px;
color: rgb(0,255,0);
}
<html>
<body>
<img src="pp.jpg" style="width:500px;height:400px">
</body>
</html>
<html>
<body>
<audio src="test.mp3" controls> </audio>
</body>
</html>
<html>
<body>
<video src="test.mp4" width="1000" height="500" controls> </video>
</body>
</html>
<html>
<body>
<svg width="1000" height="500">
<rect x="250" y="50" width="200" height="100" style="fill:rgb(255,0,0);stroke-width:10;stroke:rgb(0,255,0)"/>
<circle cx="100" cy="50" r="50" style="fill:blue"/>
</svg>
</body>
</html>
Note: SVG stands for Scalable Vector Graphics
<html>
<body>
<form action="save.php">
Name <br>
<input type="text" name="name" value="" required> <br>
<br>
<input type="radio" name="country" value="ind" checked> India <br>
<input type="radio" name="country" value="fra"> France <br>
<input type="radio" name="country" value="nor"> Norway <br>
<br>
<input type="submit" value="Save">
</form>
</body>
</html>
<html>
<body>
<script>
document.write("Hello JavaScript")
</script>
</body>
</html>
Note:
In above program, in strict sense
<script language="javascript" type="text/javascript">
should be used instead of
<script>
<html>
<body>
<p id="para1">Example for Changing HTML Content Using JavaScript</p>
<button type="button" onclick="document.getElementById('para1').innerHTML = 'You Have Changed HTML Content Using JavaScript'">
Click Here (Press F5 To Restore)
</button>
</body>
</html>
<html>
<body>
<p id="para1">Example for Changing Style of HTML Element Using JavaScript</p>
<button type="button" onclick=document.getElementById("para1").style.color="red">Click Here (Press F5 To Restore)</button>
</body>
</html>
<html>
<head>
<script>
function hai() {
alert("Hello JavaScript Function")
}
</script>
</head>
<body>
<button type="button" onclick="hai()"> Welcome JavaScript Function </button>
</body>
</html>
<html>
<head>
<script src="extjsfile.js">
</script>
</head>
<body>
<button type="button" onclick="hai()"> Welcome To JavaScript Function </button>
</body>
</html>
Note: Below content alone inside external JavaScript file ( extjsfile.js )
function hai() {
alert("Hello JavaScript Function")
}
<html>
<head>
<script>
function calFees() {
var feesamount;
if (document.forms["admit"]["scheme"].value=="regular") {
feesamount=10000;
}
if (document.forms["admit"]["scheme"].value=="payment") {
feesamount=50000;
}
if (document.forms["admit"]["scheme"].value=="nri") {
feesamount=100000;
}
if (document.forms["admit"]["hostel"].checked) {
feesamount=feesamount + 5000;
}
if (document.forms["admit"]["parking"].checked) {
feesamount=feesamount + 2000;
}
if (document.forms["admit"]["laptop"].checked) {
feesamount=feesamount + 20000;
}
document.forms["admit"]["fees"].value=feesamount;
}
</script>
</head>
<body onload="calFees()">
<form name="admit" action="admission.php" method="post">
<label> Name </label>
<input type="text" name="name" value="" required>
<br>
<br>
<label >Select Scheme</label>
<select name='scheme' onchange="calFees()">
<option value="regular"> Regular </option>
<option value="payment"> Payment </option>
<option value="nri"> NRI </option>
</select>
<br>
<br>
<label >Select Board</label> <br>
<input type="radio" name="board" value="tnboard" checked="true">TamilNadu State Board</option> <br>
<input type="radio" name="board" value="klboard">Kerala State Board</option> <br>
<input type="radio" name="board" value="apboard">Andhra State Board</option> <br>
<input type="radio" name="board" value="cbse">CBSE</option> <br>
<input type="radio" name="board" value="others">Others</option> <br>
<br>
<br>
<label >Select Need</label> <br>
<input type="checkbox" name="hostel" onchange="calFees()"> Hostel <br>
<input type="checkbox" name="parking" onchange="calFees()"> Vehicle Parking <br>
<input type="checkbox" name="laptop" onchange="calFees()"> Laptop <br>
<br>
<br>
<label> Fees </label>
<input type="text" name="fees"> <br>
<br>
<input type="submit" value="Save">
</form>
</body>
</html>
<html>
<body>
<?php
$file = fopen("admission.txt","a");
echo fwrite($file,"\n");
echo fwrite($file,$_POST["name"]);
echo fwrite($file,"\t");
echo fwrite($file,$_POST["scheme"]);
echo fwrite($file,"\t");
echo fwrite($file,$_POST["board"]);
echo fwrite($file,"\t");
echo fwrite($file,$_POST["hostel"]);
echo fwrite($file,"\t");
echo fwrite($file,$_POST["parking"]);
echo fwrite($file,"\t");
echo fwrite($file,$_POST["laptop"]);
echo fwrite($file,"\t");
echo fwrite($file,$_POST["fees"]);
echo fwrite($file,"\n");
fclose($file);
?>
</body>
</html>
Caddy - portable multiplatform webserver for HTML, CSS, JavaScript, PHP, ... Testing
{
Download it from - www.caddyserver.com
Simple Caddy Setup Without PHP Support
To Start - Just right click over the single file (given executable permission) and select Run
Listening URL - http://127.0.0.1:2015
html folder location - Just put files where caddy executable file is
To Stop - Run this command in Terminal - pkill caddy
}
Example For Client Side Scripting - JavaScript
Example For Server Side Scripting - PHP
Caddy Setup With PHP Support (checked in Ubuntu 14.04.1)
Step 1: Installing php5-fpm
sudo apt-get install php5-fpm
sudo nano /etc/php5/fpm/php.ini
Change
short_open_tag = Off
To
short_open_tag = On
sudo service php5-fpm restart
sudo service php5-fpm status
Step 2: Setting caddy with PHP Support
mkdir $HOME/caddywww
Copy the downloaded caddy executable file as below
$HOME/caddywww/caddy
nano $HOME/caddy/Caddyfile
:80
fastcgi / /var/run/php5-fpm.sock php
---
Step 3: Starting caddy with PHP Support
Copy the .html and .php files inside $HOME/caddywww/ folder
sudo chmod 755 -R $HOME/caddywww
cd $HOME/caddywww
sudo ./caddy
Firefox > 127.0.0.1
Note:
To listen in port 80, caddy has to be run using sudo
Pressing Ctrl + C or Closing Terminal Will Stop Caddy
Without sudo, caddy can serve html through other ports like 2015, but while using PHP, "502 Bad Gateway" error will appear
Simple PHP Example ( simple.php )
<html>
<body>
<?php
echo '<p>Hello World</p>';
?>
</body>
</html>
Advanced PHP Example ( admission.php ) Given Above Caddy Setup Explanation