38
1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO Compiled by Rajender Parsad 1 , B.N. Mandal 1 , A. Dhandapani 2 , Sukanta Dash 1 , Eldho Varghese 1 and D.C. Mishra 1 1 ICAR-I.A.S.R.I., Library Avenue, Pusa, New Delhi 2 ICAR-N.A.A.R.M, Rajendra Nagar, Hyderabad A. Statistical Methods A.1. Descriptive statistics Datafile: descriptive.txt v1 v2 v3 v4 12.4 45.15 1.25 1115.2 13.5 26.15 0.99 1216.4 16.3 78.14 1.63 1426.1 14.9 56.35 1.34 1635.2 14.5 65.21 0.96 1345.6 13.9 63.15 1.26 1248.3 18.2 46.15 0.64 1632.5 16.7 48.23 1.54 1458.6 23.2 86.35 1.48 1752.6 10.9 46.15 1.78 1496.8 23.9 31.25 0.69 1835.6 24.8 48.26 1.16 1934.6 26.1 46.23 1.49 1536.4 24.7 32.56 1.25 1269.7 # R-Script for obtaining Summary Statistics attach(descriptive) names(descriptive) summary(descriptive) mean1=mean(v1) mean1 sd1=sd(v1) sd1 cv1=sd1/mean1*100 cv1 meadian2=median(v2) meadian2 var(v4) v4 #obtaining summary using Library DescTools library(DescTools) mean(v1) median(v1) Mode(v1)

R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

  • Upload
    voliem

  • View
    226

  • Download
    1

Embed Size (px)

Citation preview

Page 1: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

 

R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

Compiled by Rajender Parsad1, B.N. Mandal1, A. Dhandapani2, Sukanta Dash1, Eldho Varghese1 and

D.C. Mishra1 1ICAR-I.A.S.R.I., Library Avenue, Pusa, New Delhi

2ICAR-N.A.A.R.M, Rajendra Nagar, Hyderabad

A. Statistical Methods

A.1. Descriptive statistics Datafile: descriptive.txt

v1 v2 v3 v4 12.4 45.15 1.25 1115.2 13.5 26.15 0.99 1216.4 16.3 78.14 1.63 1426.1 14.9 56.35 1.34 1635.2 14.5 65.21 0.96 1345.6 13.9 63.15 1.26 1248.3 18.2 46.15 0.64 1632.5 16.7 48.23 1.54 1458.6 23.2 86.35 1.48 1752.6 10.9 46.15 1.78 1496.8 23.9 31.25 0.69 1835.6 24.8 48.26 1.16 1934.6 26.1 46.23 1.49 1536.4 24.7 32.56 1.25 1269.7

# R-Script for obtaining Summary Statistics

attach(descriptive) names(descriptive) summary(descriptive) mean1=mean(v1) mean1 sd1=sd(v1) sd1 cv1=sd1/mean1*100 cv1 meadian2=median(v2) meadian2 var(v4) v4 #obtaining summary using Library DescTools library(DescTools) mean(v1) median(v1) Mode(v1)

Page 2: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

 

sd(v1) var(v1) CoefVar(v1) Skew(v1) Kurt(v1) Desc(descriptive) # Histogram library(Hmisc) hist(descriptive,col="red") # Density Plot dens =density(descriptive) plot(dens) #Box Plot boxplot(descriptive)

A.2 Tests of Significance Datafile: test.txt

group X Y Z P 1 7 1.85 147.7 16.86 1 7 1.86 136.86 16.77 1 6 1.83 149.97 16.35 1 7 1.89 172.33 18.26 1 7 1.8 144.46 17.9 1 6 1.88 138.3 16.95 1 7 1.89 150.58 18.15 1 7 1.79 140.99 18.86 1 6 1.85 140.57 18.39 1 7 1.84 138.33 18.58 2 6.3 2.58 224.26 18.18 2 6.7 2.74 197.5 18.07 2 7.3 2.58 230.34 19.07 2 8 2.62 217.05 19 2 8 2.68 233.84 18 2 8 2.56 216.52 18.49 2 7.7 2.34 211.93 17.45 2 7.7 2.67 210.37 18.97 2 7 2.45 199.87 19.31 2 7.3 2.44 214.3 19.36

Page 3: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

 

# R-Script for tests of significance

# Single Sample t-test (two-tailed) attach(test) names(test) t.test(X, mu=7) # t-test for comparing means of two independent populations with unequal variances t.test(group~X) # t-test for comparing means of two independent populations with equal variances t.test(group~Y,var.equal=TRUE) # Paired t-test t.test(X,Y,paired=TRUE) detach(test) A.3. Correlation Datafile: correlation.txt

pp ph ngl yld 142 0.525 8.2 2.47 143 0.64 9.5 4.76 107 0.66 9.3 3.31 78 0.66 7.5 1.97 100 0.46 5.9 1.34 86.5 0.345 6.4 1.14 103.5 0.86 6.4 1.5 155.99 0.33 7.5 2.03 80.88 0.285 8.4 2.54 109.77 0.59 10.6 4.9 61.77 0.265 8.3 2.91 79.11 0.66 11.6 2.76 155.99 0.42 8.1 0.59 61.81 0.34 9.4 0.84 74.5 0.63 8.4 3.87 97 0.705 7.2 4.47 93.14 0.68 6.4 3.31 37.43 0.665 8.4 1.57 36.44 0.275 7.4 0.53 51 0.28 7.4 1.15 104 0.28 9.8 1.08 49 0.49 4.8 1.83 54.66 0.385 5.5 0.76 55.55 0.265 5 0.43 88.44 0.98 5 4.08 99.55 0.645 9.6 2.83 63.99 0.635 5.6 2.57 101.77 0.29 8.2 7.42 138.66 0.72 9.9 2.62 90.22 0.63 8.4 2

Page 4: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

 

76.92 1.25 7.3 1.99 126.22 0.58 6.9 1.36 80.36 0.605 6.8 0.68 150.23 1.19 8.8 5.36 56.5 0.355 9.7 2.12 136 0.59 10.2 4.16 144.5 0.61 9.8 3.12 157.33 0.605 8.8 2.07 91.99 0.38 7.7 1.17 121.5 0.55 7.7 3.62 64.5 0.32 5.7 0.67 116 0.455 6.8 3.05 77.5 0.72 11.8 1.7 70.43 0.625 10 1.55 133.77 0.535 9.3 3.28 89.99 0.49 9.8 2.69 # R-Script for Correlation

attach(correlation) names(correlation) # Correlation among all possible pairs of variables cor(correlation, method="pearson") # Correlation between specific pairs of variables corr=cor.test(pp,ph) str(corr) # Testing the significance of Correlation Coefficient corr$statistic corr$p.value corr$conf.int detach(correlation) A.4. Regression Analysis

Datafile: reg.txt Y X1 X2 X3 2.76 79.11 0.66 11.6 0.59 155.99 0.42 8.1 0.84 61.81 0.34 9.4 3.87 74.5 0.63 8.4 4.47 97 0.705 7.2 3.31 93.14 0.68 6.4 1.57 37.43 0.665 8.4 0.53 36.44 0.275 7.4 1.15 51 0.28 7.4 1.08 104 0.28 9.8 1.83 49 0.49 4.8 0.76 54.66 0.385 5.5

Page 5: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

 

0.43 55.55 0.265 5 4.08 88.44 0.98 5 2.83 99.55 0.645 9.6 2.57 63.99 0.635 5.6 7.42 101.77 0.29 8.2 2.62 138.66 0.72 9.9 2 90.22 0.63 8.4 1.99 76.92 1.25 7.3 1.36 126.22 0.58 6.9 0.68 80.36 0.605 6.8 5.36 150.23 1.19 8.8 2.12 56.5 0.355 9.7 4.16 136 0.59 10.2 3.12 144.5 0.61 9.8 2.07 157.33 0.605 8.8 1.17 91.99 0.38 7.7 3.62 121.5 0.55 7.7 0.67 64.5 0.32 5.7 3.05 116 0.455 6.8 1.7 77.5 0.72 11.8 1.55 70.43 0.625 10

# R-Script for Regression Analysis

attach(reg) names(reg) lm1 =lm(Y~X1+X2+X3, reg) summary(lm1) plot(lm1) #Test for Non-Constant Variance - quick way from fitted values as in lm1 summary(lm(abs(residuals(lm1)) ~ fitted(lm1))) # Testing the Normality Residuals Using Shapiro-Wilk Test hist(residuals(lm1)) boxplot(residuals(lm1)) shapiro.test(residuals(lm1)) #test of independence of residuals using Durbin Watson test library("lmtest") dwtest(lm1) #influential observations using Hat matrix influence1 =influence(lm1) summary(influence1) summary(influence1$hat)

Page 6: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

 

# Looking for multi-collinearity library(car) vif(lm1) detach(reg) B. Design of Experiments B.1. Completely Randomized Designs datafile: crd.txt trt yield 1 850.5 1 453.6 1 878.85 1 623.7 1 510.3 1 765.45 1 680.4 1 595.35 1 538.65 1 850.5 1 850.5 1 793.8 1 1020.6 1 708.75 1 652.05 1 623.7 1 396.9 1 822.15 1 680.4 1 652.05 1 538.65 1 850.5 1 680.4 2 510.3 2 963.9 2 652.05 2 1020.6 2 878.85 2 567 2 680.4 2 538.65 2 567 2 510.3 2 425.25 2 567 2 623.7 2 538.65 2 737.1 2 453.6

Page 7: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

 

2 481.95 2 368.55 2 567 2 595.35 2 567 2 595.35 3 992.25 3 850.5 3 1474.2 3 510.3 3 850.5 3 793.8 3 453.6 3 935.55 3 1190.7 3 481.95 3 623.7 3 878.85 3 1077.3 3 850.5 3 680.4 3 737.1 3 737.1 3 708.75 3 708.75 3 652.05 3 567 3 453.6 3 652.05 3 567 # R-Script for CRD

attach(crd) names(crd) trt=factor(trt) lm1=lm(yield~trt) anova(lm1) library(lsmeans) lsm=lsmeans(lm1,"trt") lsm pairs(lsm) #pairs statement is an optional statement #to provide letters for groups, need to install multcompView library(multcompView) cld(lsm,Letters="abc") detach(crd)

Page 8: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

 

B.2. Randomized Complete Block Designs

Datafile: rbd.txt trt blk yield 1 1 1539.69 2 1 1261.85 3 1 1389.19 4 1 1192.39 5 1 1250.27 6 1 1296.58 7 1 1227.12 8 1 1273.43 9 1 1180.82 10 1 1296.58 11 1 1122.93 12 1 1250.27 13 1 1180.82 14 1 1146.09 15 1 1574.42 16 1 914.55 17 1 891.4 18 1 1227.12 19 1 1389.19 20 1 1331.31 21 1 1250.27 22 1 717.75 23 1 1169.24 24 1 1203.97 1 2 1412.35 2 2 1065.05 3 2 1516.54 4 2 1215.55 5 2 1203.97 6 2 1273.43 7 2 1018.74 8 2 1157.66 9 2 1203.97 10 2 1458.65 11 2 1065.05 12 2 926.13 13 2 1053.47 14 2 1180.82 15 2 1412.35 16 2 972.44 17 2 937.71 18 2 1203.97 19 2 1180.82 20 2 1157.66 21 2 1250.27 22 2 740.9

Page 9: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

 

23 2 1157.66 24 2 1296.58 1 3 1319.73 2 3 1111.36 3 3 1203.97 4 3 1157.66 5 3 1366.04 6 3 1308.16 7 3 937.71 8 3 1088.2 9 3 1041.9 10 3 1250.27 11 3 1018.74 12 3 1030.32 13 3 717.75 14 3 856.67 15 3 1597.57 16 3 659.87 17 3 798.79 18 3 1389.19 19 3 1273.43 20 3 1180.82 21 3 1296.58 22 3 578.83 23 3 1111.36 24 3 1250.27 # R-Script for RCB Design (it is same for any block design)

attach(rbd) names(rbd) trt=factor(trt) blk=factor(blk) lm1=lm(yield~trt+blk) anova(lm1) library(lsmeans) lsm=lsmeans(lm1,"trt") lsm pairs(lsm) #pairs statement is an optional statement #to provide letters for groups, need to install multcompView library(multcompView) cld(lsm, Letters="abcdefghij") detach(rbd)

Page 10: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

10 

 

B.3. Resolvable Block Designs datafile: resolvable.txt rep blk trt syield 1 1 1 1555.6 1 1 5 1160.5 1 1 9 1308.6 1 1 13 1382.7 1 1 17 987.7 1 1 21 1135.8 1 2 2 1284 1 2 6 1086.4 1 2 10 1284 1 2 14 1111.1 1 2 18 938.3 1 2 22 1308.6 1 3 3 1234.6 1 3 7 419.8 1 3 11 1308.6 1 3 15 963 1 3 19 963 1 3 23 987.7 1 4 4 1234.6 1 4 8 987.7 1 4 12 1284 1 4 16 913.6 1 4 20 1160.5 1 4 24 790.1 2 1 1 1481.5 2 1 6 1086.4 2 1 11 1308.6 2 1 16 1284 2 1 19 1111.1 2 1 22 1185.2 2 2 2 987.7 2 2 7 308.6 2 2 12 1234.6 2 2 13 1308.6 2 2 20 765.4 2 2 23 938.3 2 3 3 1012.3 2 3 8 864.2 2 3 9 1234.6 2 3 14 938.3 2 3 17 913.6 2 3 24 864.2 2 4 4 1135.8 2 4 5 987.7 2 4 10 987.7 2 4 15 740.7

Page 11: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

11 

 

2 4 18 963 2 4 21 1135.8 3 1 1 1284 3 1 7 333.3 3 1 12 1135.8 3 1 15 839.5 3 1 18 814.8 3 1 24 888.9 3 2 2 1135.8 3 2 8 913.6 3 2 9 1456.8 3 2 16 1037 3 2 19 938.3 3 2 21 1037 3 3 3 963 3 3 5 1209.9 3 3 10 1259.3 3 3 13 1234.6 3 3 20 963 3 3 22 1111.1 3 4 4 1086.4 3 4 6 765.4 3 4 11 1111.1 3 4 14 1037 3 4 17 938.3 3 4 23 938.3 # R-Script for Resolvable block design or alpha design (infact similar steps can be used for nested block designs)

attach(resolvable) names(resolvable) trt=factor(trt) rep=factor(rep) blk=factor(blk) lm1=lm(syield~trt+blk+rep/blk-blk) #anova(lm1) library(car) Anova(lm1,type="III") library(lsmeans) lsm=lsmeans(lm1,"trt") lsm pairs(lsm) #pairs statement is an optional statement #to provide letters for groups, need to install multcompView library(multcompView) cld(lsm,Letters="abcdefghij") detach(resolvable)

Page 12: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

12 

 

B.4: Latin Square Design (steps are similar for any row-column design)

Datafile: latin.txt row col trt yield 1 1 3 3.1 1 2 6 5.95 1 3 1 1.75 1 4 5 6.4 1 5 2 3.85 1 6 4 5.3 2 1 2 4.8 2 2 1 2.7 2 3 3 3.3 2 4 6 5.95 2 5 4 3.7 2 6 5 5.4 3 1 1 3 3 2 2 2.95 3 3 5 6.7 3 4 4 5.95 3 5 6 7.75 3 6 3 7.1 4 1 5 6.4 4 2 4 5.8 4 3 2 3.8 4 4 3 6.55 4 5 1 4.8 4 6 6 9.4 5 1 6 5.2 5 2 3 4.85 5 3 4 6.6 5 4 2 4.6 5 5 5 7 5 6 1 5 6 1 4 4.25 6 2 5 6.65 6 3 6 9.3 6 4 1 4.95 6 5 3 9.3 6 6 2 8.4 # R-Script for Latin Square Design (steps are similar for any row-column design) 

attach(latin) names(latin) row=factor(row) col=factor(col) trt=factor(trt) lm1=lm(yield~trt+row+col) anova(lm1)

Page 13: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

13 

 

library(lsmeans) lsm=lsmeans(lm1,"trt") lsm pairs(lsm) #pairs statement is an optional statement #to provide letters for groups, need to install multcompView library(multcompView) cld(lsm, Letters="abcdefghij") detach(latin) B.5 Treatment Contrast Analysis in a Block Design (similarly one can write contrasts for any design

datafile: tree1.txt rep trt height 1 1 144.44 1 2 113.5 1 3 60.88 1 4 163.44 1 5 110.11 1 6 260.05 1 7 114 1 8 91.94 1 9 156.11 1 10 80.2 2 1 145.11 2 2 118.61 2 3 90.94 2 4 158.55 2 5 116 2 6 102.27 2 7 115.16 2 8 58.16 2 9 177.97 2 10 108.05 3 1 104 3 2 118.61 3 3 80.33 3 4 158.88 3 5 119.66 3 6 256.22 3 7 114.88 3 8 76.83 3 9 148.22 3 10 45.18 4 1 105.44 4 2 123

Page 14: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

14 

 

4 3 92 4 4 153.11 4 5 103.22 4 6 217.8 4 7 106.33 4 8 79.5 4 9 183.17 4 10 79.55 # R-Script for Treatment Contrast Analysis (steps are similar for any design) 

attach(tree1) names(tree1) tree=factor(trt) rep=factor(rep) lm1=lm(height~rep+tree) anova(lm1) #contrast analysis library(lsmeans) lsm = lsmeans(lm1, "tree") lsm pairs(lsm) #pairs statement is an optional statement #For grouping of treatments with letters, you need to install multcompView package and run the following code cld(lsm,Letters="ABCDEFGHI", sort="false") contrast(lsm, list(con1 = c(1,1,1,1,-1,-1,-1,-1,-1,1),con2 = c(9,-1,-1,-1,-1,-1,-1,-1,-1,-1),con3 = c(1,1,1,1,0,0,0,0,-4,0),con4 = c(1,1,1,1,0,0,0,0,0,-4),con5 = c(0,0,0,0,1,1,1,1,-4,0),con6 = c(0,0,0,0,1,1,1,1,0,-4))) detach(tree1) B.6. Analysis of Covariance in RCB Design Datafile: ANCOVA.txt trt rep x y 1 1 5 2 2 1 6 4 3 1 5 4 4 1 6 3 5 1 7 7 6 1 6 4 7 1 6 3 8 1 6 6 9 1 7 4 10 1 7 7 11 1 6 5 12 1 6 5 13 1 5 4 14 1 5 5

Page 15: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

15 

 

15 1 5 4 1 2 6 3 2 2 5 3 3 2 5 4 4 2 5 3 5 2 7 6 6 2 5 3 7 2 5 3 8 2 7 7 9 2 5 3 10 2 7 7 11 2 5 4 12 2 5 3 13 2 5 4 14 2 5 4 15 2 5 5 1 3 6 4 2 3 5 3 3 3 5 3 4 3 5 3 5 3 6 6 6 3 5 3 7 3 6 3 8 3 6 6 9 3 5 4 10 3 5 6 11 3 5 5 12 3 5 3 13 3 6 5 14 3 5 3 15 3 6 6 # R-Script for ANCOVA in RCB Design (steps are similar for any design) attach(ANCOVA) trt=factor(trt) #ancova with class variable treatments and contiuous initial body weight variable lm3=lm(y~trt+rep+x) anova(lm3) library(lsmeans) lsm=lsmeans(lm3,"trt") lsm pairs(lsm) #pairs statement is an optional statement Library(multcompView) cld(lsm,Letters="abcdefgh") detach(ANCOVA)

Page 16: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

16 

 

B.7 Randomized Complete Block Design with Factorial Treatment Structure (Factorial RCB Design) Datafile: fact.txt REP FYM P PSB TRT Yield 1 1 1 1 1 0.7 1 1 1 2 2 1.13 1 1 2 1 3 1.23 1 1 2 2 4 1.25 1 1 3 1 5 1.25 1 1 3 2 6 1.25 1 2 1 1 7 0.83 1 2 1 2 8 1.23 1 2 2 1 9 1.18 1 2 2 2 10 0.88 1 2 3 1 11 1.63 1 2 3 2 12 1.48 2 1 1 1 1 0.98 2 1 1 2 2 1.13 2 1 2 1 3 1.18 2 1 2 2 4 1.13 2 1 3 1 5 1.26 2 1 3 2 6 1.25 2 2 1 1 7 0.93 2 2 1 2 8 0.88 2 2 2 1 9 1.5 2 2 2 2 10 1.3 2 2 3 1 11 1.38 2 2 3 2 12 1.43 3 1 1 1 1 0.9 3 1 1 2 2 1.1 3 1 2 1 3 1.1 3 1 2 2 4 0.88 3 1 3 1 5 1.35 3 1 3 2 6 1.35 3 2 1 1 7 1.1 3 2 1 2 8 1.03 3 2 2 1 9 1.3 3 2 2 2 10 0.88 3 2 3 1 11 1.38 3 2 3 2 12 1.43 4 1 1 1 1 0.73 4 1 1 2 2 1.25 4 1 2 1 3 1.43 4 1 2 2 4 1.25 4 1 3 1 5 1.1 4 1 3 2 6 1.75

Page 17: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

17 

 

4 2 1 1 7 0.98 4 2 1 2 8 1.38 4 2 2 1 9 1.35 4 2 2 2 10 1.43 4 2 3 1 11 1.3 4 2 3 2 12 1.5 # R-Script for Factorial RCB Design (steps are similar for partially confounded factorial experiments) 

attach(fact) names(fact) rep=factor(REP) fym=factor(FYM) p=factor(P) psb=factor(PSB) lm1=lm(Yield~rep+fym+p+psb+fym:p+fym:psb+p:psb+fym:p:psb) lm1 anova(lm1) library(lsmeans) lsm1=lsmeans(lm1,"fym") lsm1 #All pairs statement are optional pairs(lsm1) lsm2=lsmeans(lm1,"p") lsm2 pairs(lsm2) lsm3=lsmeans(lm1,"psb") lsm3 pairs(lsm3) lsm4=lsmeans(lm1,~fym:p) lsm4 pairs(lsm4) lsm5=lsmeans(lm1,~fym:psb) lsm5 pairs(lsm5) lsm6=lsmeans(lm1,~p:psb) lsm6 pairs(lsm6) lsm7=lsmeans(lm1,~fym:p:psb) lsm7 pairs(lsm7) #to provide letters for groups, need to install multcompView library(multcompView) cld(lsm1, Letters="ABCDEF") cld(lsm2, Letters="ABCDEF") cld(lsm3,Letters="ABCDEF") cld(lsm4,Letters="ABCDEFGHI") cld(lsm5,Letters="ABCDEFGHI")

Page 18: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

18 

 

cld(lsm6,Letters="ABCDEFGHI") cld(lsm7,Letters="ABCDEFGHIJKLM") detach(fact) B.8: Fractional factorial Experiments Datafile: fractional.txt trt A B C D E F X (1) 0 0 0 0 0 0 15 ab 1 1 0 0 0 0 4 ac 1 0 1 0 0 0 7 ad 1 0 0 1 0 0 12 ae 1 0 0 0 1 0 13 af 1 0 0 0 0 1 15 bc 0 1 1 0 0 0 17 bd 0 1 0 1 0 0 5 be 0 1 0 0 1 0 6 bf 0 1 0 0 0 1 14 cd 0 0 1 1 0 0 9 ce 0 0 1 0 1 0 12 cf 0 0 1 0 0 1 11 de 0 0 0 1 1 0 5 df 0 0 0 1 0 1 13 ef 0 0 0 0 1 1 6 abcd 1 1 1 1 0 0 3 abce 1 1 1 0 1 0 11 abcf 1 1 1 0 0 1 6 abde 1 1 0 1 1 0 4 abdf 1 1 0 1 0 1 10 abef 1 1 0 0 1 1 4 acde 1 0 1 1 1 0 9 acdf 1 0 1 1 0 1 1 acef 1 0 1 0 1 1 8 adef 1 0 0 1 1 1 1 bcde 0 1 1 1 1 0 6 bcdf 0 1 1 1 0 1 10 bdef 0 1 0 1 1 1 5 bcef 0 1 1 0 1 1 2 cdef 0 0 1 1 1 1 1 abcdef 1 1 1 1 1 1 5 # R-Script for Fractional Factorial Experiments for Estimating Main Effects and Two-factor Interactions  

attach(fractional) names(fractional) a=factor(A) b=factor(B) c=factor(C) d=factor(D) e=factor(E)

Page 19: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

19 

 

f=factor(F) lm1=lm(X~a+b+c+d+e+f+a:b+a:c+a:d+a:e+a:f+b:c+b:d+b:e+b:f +c:d+c:e+c:f+d:e+d:f+e:f) anova(lm1) library(lsmeans) lsm1=lsmeans(lm1,"a") lsm1 #All pairs statement are optional pairs(lsm1) lsm2=lsmeans(lm1,"b") lsm2 pairs(lsm2) lsm3=lsmeans(lm1,"c") lsm3 pairs(lsm3) lsm4=lsmeans(lm1,"d") lsm4 pairs(lsm4) lsm5=lsmeans(lm1,"e") lsm5 pairs(lsm5) lsm6=lsmeans(lm1,"f") lsm6 pairs(lsm6) lsm7=lsmeans(lm1,~a:b) lsm7 pairs(lsm7) lsm8=lsmeans(lm1,~a:c) lsm8 pairs(lsm8) lsm9=lsmeans(lm1,~a:d) lsm9 pairs(lsm9) lsm10=lsmeans(lm1,~a:e) lsm10 pairs(lsm10) lsm11=lsmeans(lm1,~a:f) lsm11 pairs(lsm11) lsm12=lsmeans(lm1,~b:c) lsm12 pairs(lsm12) lsm13=lsmeans(lm1,~b:d) lsm13 pairs(lsm13) lsm14=lsmeans(lm1,~b:e) lsm14 pairs(lsm14)

Page 20: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

20 

 

lsm15=lsmeans(lm1,~b:f) lsm15 pairs(lsm15) lsm16=lsmeans(lm1,~c:d) lsm16 pairs(lsm16) lsm17=lsmeans(lm1,~c:e) lsm17 pairs(lsm17) lsm18=lsmeans(lm1,~c:f) lsm18 pairs(lsm18) lsm19=lsmeans(lm1,~d:e) lsm19 pairs(lsm19) lsm20=lsmeans(lm1,~d:f) lsm20 pairs(lsm20) lsm21=lsmeans(lm1,~e:f) lsm21 pairs(lsm21) library(multcompView) cld(lsm1,Letters="ab") cld(lsm2,Letters="ab") cld(lsm3,Letters="ab") cld(lsm4,Letters="ab") cld(lsm5,Letters="ab") cld(lsm6,Letters="ab") cld(lsm7,Letters="abcd") cld(lsm8,Letters="abcd") cld(lsm9,Letters="abcd") cld(lsm10,Letters="abcd") cld(lsm11,Letters="abcd") cld(lsm12,Letters="abcd") cld(lsm13,Letters="abcd") cld(lsm14,Letters="abcd") cld(lsm15,Letters="abcd") cld(lsm16,Letters="abcd") cld(lsm17,Letters="abcd") cld(lsm18,Letters="abcd") cld(lsm19,Letters="abcd") cld(lsm20,Letters="abcd") cld(lsm21,Letters="abcd") 

Page 21: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

21 

 

B.9: Split Plot Design Datafile: plots in the package library library(agricolae) data(plots) str(plots) plots[,1] <-as.factor(plots[,1]) # split-plot analysis model <- aov(yield ~ block + A + Error(plot)+ B + A:B, data=plots) summary(model) attach(plots) b<-nlevels(B) a<-nlevels(A) r<-nlevels(block) dfa <- df.residual(model$plot) Ea <-deviance(model$plot)/dfa dfb <- df.residual(model$Within) Eb <-deviance(model$Within)/dfb Eab <- (Ea +(b-1)*Eb)/(b*r) #dfab<-(Ea +(b-1)*Eb)^2/(Ea^2/dfa +((b-1)*Eb)^2/dfb) # Comparison A, A(b1), A(b2), A(b3) title1= "CD at 5% for main plot means" comparison1 <-LSD.test(yield,A,dfa,Ea) title1 Comparison1 title2="CD at 5% for sub plot means" comparison2 <-LSD.test(yield,B,dfb,Eb) title2 comparison2 title3="CD at 5% for sub plot means at same level main plot" sd3=sqrt(2*Eb/r) cd3=qt(0.975,dfb)*sd3 title3 cd3 #comparison6 <-LSD.test(yield[A=="a1"],B[A=="a1"],dfb,Eb) #comparison7 <-LSD.test(yield[A=="a2"],B[A=="a2"],dfb,Eb) #comparison8 <-LSD.test(yield[B=="b1"],A[B=="b2"],dfab,Eab) title4= "calculation Main Plot Means at same or different levels of subplot treatment" ab <- sqrt(2*((b-1)*Eb+Ea)/(b*r)) tval <- ((b-1)*Eb*qt(0.975,dfb)+Ea*qt(0.975,dfa))/((b-1)*Eb + Ea) title4= CD4 <- ab*tval title4 CD4 plots detach(plots)

Page 22: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

22 

 

B.10: Strip Plot Design Datafile: STRIP.txt rep A B yield 1 1 1 103.6 1 1 2 126.25 1 1 3 115.39 1 2 1 103.8 1 2 2 119.32 1 2 3 106.4 1 3 1 138.3 1 3 2 132.81 1 3 3 128.9 2 1 1 91.65 2 1 2 103.3 2 1 3 101.56 2 2 1 116.65 2 2 2 125.4 2 2 3 144.65 2 3 1 106.6 2 3 2 129.35 2 3 3 138.3 3 1 1 126.91 3 1 2 119.9 3 1 3 134.19 3 2 1 131.19 3 2 2 119.73 3 2 3 127.11 3 3 1 125.8 3 3 2 113.8 3 3 3 125.35 4 1 1 129.61 4 1 2 118.39 4 1 3 123 4 2 1 120.81 4 2 2 120.26 4 2 3 125.6 4 3 1 104.71 4 3 2 135.6 4 3 3 114.31 5 1 1 128.29 5 1 2 129.4 5 1 3 123.71 5 2 1 131.29 5 2 2 122.85 5 2 3 138.2 5 3 1 123.31 5 3 2 142.71 5 3 3 152.21 6 1 1 121.06

Page 23: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

23 

 

6 1 2 112.95 6 1 3 115.91 6 2 1 119.6 6 2 2 122.45 6 2 3 126.61 6 3 1 126.96 6 3 2 130.94 6 3 3 131.7 #R Script for Strip Plot Design attach(STRIP) names(STRIP) block=factor(rep) A=factor(A) B=factor(B) library(agricolae) model<-strip.plot(block, A, B, yield) Ea = model$Ea Eb = model$Eb Ec = model$Ec b<-nlevels(A) a<-nlevels(B) r<-nlevels(block) dfa = model$gl.a dfb = model$gl.b dfc = model$gl.c #CD Vertical Plot means vsed = sqrt(2*Ea/(r*b)) cd1 = vsed * qt(0.975,dfa) #CD Horizontal Plot means hsed = sqrt(2*Eb/(r*a)) cd2 = hsed *qt(0.975,dfb) #Two vertical plot means at same or different levels of horizontal plot means sd1 = sqrt(2*((b-1)*Ec+Ea)/(r*b)) twb = ((b-1)*Ec*qt(0.975,dfc)+Ea*qt(0.975,dfa))/((b-1)*Ec+Ea) cd3 = sd1*twb # Two horizontal plot means at same or different levels of vertical plot means sd2 = sqrt(2*((a-1)*Ec+Eb)/(r*a)) twa = ((a-1)*Ec+qt(0.975,dfc)+Eb*qt(0.975,dfb))/((a-1)*Ec+Eb) cd4 =sd2*twa title1 = "CD for vertical plot means" title1 cd1 title2 = "CD for horizontal plot means" title2

Page 24: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

24 

 

cd2 title3 = "CD two vertical plot means at the same or different levels of horizontal plot means" title3 cd3 title4 = "CD for horizontal plot means at the same or different levels of vertical plot means" title4 cd4 detach(STRIP) B.11: Groups of Experiments conducted across locations in a block design datafile: COMINED2.txt loc rep varn syield 1 1 1 1794 1 1 2 1134 1 1 3 718 1 1 4 1852 1 1 5 2245 1 1 6 1111 1 1 7 1181 1 1 8 1644 1 1 9 1551 1 1 10 1968 1 1 11 2662 1 1 12 1065 1 2 1 2014 1 2 2 1736 1 2 3 764 1 2 4 1551 1 2 5 2361 1 2 6 1065 1 2 7 880 1 2 8 1991 1 2 9 1435 1 2 10 1551 1 2 11 2338 1 2 12 1227 1 3 1 2581 1 3 2 1898 1 3 3 880 1 3 4 1887 1 3 5 2407 1 3 6 1111 1 3 7 1528 1 3 8 2060 1 3 9 1991 1 3 10 2569 1 3 11 3056 1 3 12 1343 2 1 1 2600

Page 25: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

25 

 

2 1 2 3289 2 1 3 2756 2 1 4 2600 2 1 5 2689 2 1 6 2578 2 1 7 3178 2 1 8 3244 2 1 9 2444 2 1 10 3156 2 1 11 2667 2 1 12 2689 2 2 1 2444 2 2 2 2667 2 2 3 2511 2 2 4 2444 2 2 5 2422 2 2 6 2400 2 2 7 3044 2 2 8 2911 2 2 9 2222 2 2 10 2978 2 2 11 2267 2 2 12 2444 2 3 1 2711 2 3 2 2889 2 3 3 2400 2 3 4 2222 2 3 5 2444 2 3 6 2222 2 3 7 2889 2 3 8 3111 2 3 9 2667 2 3 10 2756 2 3 11 2111 2 3 12 2289 3 1 1 3286 3 1 2 2518 3 1 3 757 3 1 4 2553 3 1 5 2908 3 1 6 1797 3 1 7 1749 3 1 8 1501 3 1 9 1513 3 1 10 2447 3 1 11 2600 3 1 12 1631 3 2 1 2459 3 2 2 2364

Page 26: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

26 

 

3 2 3 993 3 2 4 2388 3 2 5 2482 3 2 6 1560 3 2 7 1537 3 2 8 2317 3 2 9 1608 3 2 10 2459 3 2 11 2884 3 2 12 1466 3 3 1 3286 3 3 2 2364 3 3 3 875 3 3 4 2884 3 3 5 2884 3 3 6 2033 3 3 7 1537 3 3 8 2577 3 3 9 2104 3 3 10 2813 3 3 11 2648 3 3 12 1844 4 1 1 1370 4 1 2 904 4 1 3 858 4 1 4 904 4 1 5 1438 4 1 6 873 4 1 7 848 4 1 8 1668 4 1 9 910 4 1 10 1558 4 1 11 1508 4 1 12 1280 4 2 1 1209 4 2 2 729 4 2 3 942 4 2 4 959 4 2 5 1456 4 2 6 959 4 2 7 639 4 2 8 1770 4 2 9 907 4 2 10 1606 4 2 11 1389 4 2 12 1207 4 3 1 1320 4 3 2 1007 4 3 3 839

Page 27: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

27 

 

4 3 4 1155 4 3 5 1695 4 3 6 946 4 3 7 643 4 3 8 1607 4 3 9 1081 4 3 10 1705 4 3 11 1447 4 3 12 1256 5 1 1 2233 5 1 2 2222 5 1 3 2000 5 1 4 2667 5 1 5 2444 5 1 6 1778 5 1 7 1778 5 1 8 3000 5 1 9 1778 5 1 10 3778 5 1 11 3111 5 1 12 2222 5 2 1 2222 5 2 2 2444 5 2 3 1778 5 2 4 3289 5 2 5 2000 5 2 6 1889 5 2 7 1722 5 2 8 2889 5 2 9 1611 5 2 10 3667 5 2 11 3111 5 2 12 2000 5 3 1 2222 5 3 2 2722 5 3 3 1778 5 3 4 3333 5 3 5 2000 5 3 6 1556 5 3 7 1722 5 3 8 3222 5 3 9 1333 5 3 10 3556 5 3 11 3222 5 3 12 2222 6 1 1 1666 6 1 2 1611 6 1 3 1389 6 1 4 1511

Page 28: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

28 

 

6 1 5 1644 6 1 6 1833 6 1 7 1788 6 1 8 1644 6 1 9 1889 6 1 10 2000 6 1 11 944 6 1 12 1488 6 2 1 1333 6 2 2 1389 6 2 3 1244 6 2 4 1778 6 2 5 1622 6 2 6 1822 6 2 7 2333 6 2 8 2220 6 2 9 1822 6 2 10 1556 6 2 11 388 6 2 12 1400 6 3 1 2222 6 3 2 1944 6 3 3 2056 6 3 4 1889 6 3 5 1711 6 3 6 2111 6 3 7 1711 6 3 8 2220 6 3 9 2444 6 3 10 1356 6 3 11 722 6 3 12 1356 #R-Script for Groups of Experiments attach(COMBINED2) names(COMBINED2) loc=factor(loc) levels(loc) varn=factor(varn) rep=factor(rep) #Individual Analysis out=by(COMBINED2,COMBINED2[,"loc"],function(x) aov(syield~factor(rep)+factor(varn),data=x)) out=as.list(out) sapply(out,summary) lapply(out,TukeyHSD,"factor(varn)") MSE=sapply(1:6,function(i,out) sum(out[[i]]$residual^2)/out[[i]]$df.residual,out)

Page 29: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

29 

 

residfs=sapply(1:6,function(i,out) out[[i]]$df.residual,out) #Check homogeneity of error variances across locations bartlett.test(out) #Transform the observations as variances are heterogeneous tsyield=unlist(tapply(syield,loc,function(syield,MSE) syield/sqrt(MSE),MSE)) #for combined anova lm2=lm(tsyield~loc+varn+loc/rep-rep+loc:varn) anova(lm2) library(lsmeans) lsm1=lsmeans(lm2,"varn") cld(lsm1, Letters="abcdefghijklmn") #Obtaining AMMI Biplot library(agricolae) model<- AMMI(loc, varn, rep, tsyield) # biplot PC2 vs PC1 model$biplot plot(model) detach(COMBINED2)

B.12: Response Surface Methodology Datafile: rsd.txt N S yield 0 0 4121.212 0 20 4678.03 0 40 4742.424 0 60 4727.273 50 0 6083.333 50 20 6041.667 50 40 6223.485 50 60 6715.909 100 0 6761.364 100 20 6916.667 100 40 6852.273 100 60 6810.606 150 0 6174.242 150 20 7022.727 150 40 7003.788 150 60 6943.182

# R-Script for Response Surface Methodology

# Package required is "rsm" /* response surface analysis*/ /* FO = first order model and SO =second order model*/ library(rsm)

Page 30: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

30 

 

attach(rsd) anal= rsm(yield~ SO(N,S), data=rsd) summary(anal) detach(rsd) B.13: Experiments With Mixtures Methodology Datafile: mixture.txt x1 x2 x3 Yield 1 0 0 11.2 0.75 0.25 0 12.33 0.75 0 0.25 14.04 0.5 0.5 0 16.41 0.5 0 0.5 18.26 0.5 0.25 0.25 16.89 0.25 0.75 0 18.22 0.25 0.5 0.25 19.42 0 1 0 20.61 0 0.75 0.25 19.93 0 0.5 0.5 20.77 # R-Script for Mixture Experiments attach(mixture names(mixture) fit=lm(Yield~x1+x2+x3+x1:x2+x1:x3+x2:x3) summary(fit) coefficients(fit) confint(fit, level=0.95) fitted(fit) residuals(fit) anova(fit) vcov(fit) influence(fit) detach(mixture) Note: In analysis of data generated from designed experiments, the R Scripts given above, makes use of lm and library(lsmeans) and library(multcompView). In these scripts, the adjusted means for treatment effects are obtained. In case of balanced and orthogonal designs, the adjusted means are same as original means. For multiple comparison procedure, the default option used is Tukey's Honest Significant Difference Test at 5% level of significance. If one is analysing data from balanced and orthogonal designs, then one may use different options for multiple comparisons in agricolae R Package.

Page 31: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

31 

 

C. Non-parametric Tests (For More Details on Examples, please refer to lecture notes) C.1. Binomial test Suppose for example x= 8 and n=38, the test is as follows: R Script: binom.test(8, 38, p = 0.5) C.2. Chi-square Test for Goodness of Fit A random sample of students enrolled in Statistics 101 at ABC University was taken. It consists of the following: there are 25 freshman in the sample, 32 sophomores, 18 juniors, and 20 seniors. Test the null hypothesis that freshman, sophomores, juniors, and seniors are equally represented among students signed up for Stat 101. (a) R Script (This is a goodness of fit test with equal expected frequencies): chisq.test(c(25,32,18,20)) (b) R Script (This is a goodness of fit test with unequal expected frequencies): ofs <- c(25,32,18,20) null.probs <- c(1/3,1/3,1/6,1/6) chisq.test(ofs, p=null.probs) C.3. Chi-square Test for Independence of Attributes R-Script: library(MASS) # load the MASS package attach(survey) tbl = table(survey$Smoke, survey$Exer) tbl # the contingency table Freq None Some Heavy 7 1 3 Never 87 18 84 Occas 12 3 4 Regul 9 1 7 chisq.test(tbl) C.4. Run test First download and install the package named lawstat . Then use runs.test(x) R Script: y<-c(5.2, 5.5,3.8,2.5,8.3,2.1,1.7,10,10,6.9,7.5,10.6) runs.test(y) C.5. Median test R Script x1 <- c(1.1, 2.1, 1.7, 1.6, 1.9, 1.3) x2 <- c(1.0, 1.2, 0.7, 0.6, 0.9, 0.5) m <- median(c(x1,x2)) # joint median f11 <- sum(x1>m) # Pop.1 samples above median

Page 32: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

32 

 

f12 <- sum(x2>m) f21 <- sum(x1<=m) # Pop.1 samples below or at median f22 <- sum(x2<=m) table <- matrix(c(f11,f12,f21,f22), nrow=2,ncol=2) # 2x2 contingency table chisq.test(table) C.6. Wilcoxon Signed Rank Test for Matched Pairs First load the package named ‘exactRankTests’ R Script: b<-c(80,80,92,79,92,82,88,89,92,83) a<-c(86,84,78,90,92,77,89,90,90,86) wilcox.exact(a,b, paired = TRUE, alternative = "two.sided") C.7. Kolmogorov-Smirnov Two Sample Test R Script: a<-c(40,30,40,45,55,30 ) b<-c(50,50,45,55,60,40 ) ks.test(a,b) C.8. Kruskal-Wallis One-Way ANOVA Import the data saved in Excel into R by loading the packages "xlsxjars rjava xlsx". OR Import text file into R-studio Prepare data file as follows: mydata.txt group response 1 23 1 26 1 51 1 49 1 58 1 37 1 29 1 44 2 22 2 27 2 39 2 29 2 46 2 48 2 49 2 65 3 59 3 66 3 38 3 49

Page 33: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

33 

 

3 56 3 60 3 56 3 62 R Script: attach(mydata) fix(mydata) kruskal.test(response ~ group, data = mydata) D. Multivariate Techniques D.1: Principal Component Analysis Datafile: pca.txt sn pp ph ngl yld 1 142 0.525 8.2 2.47 2 143 0.64 9.5 4.76 3 107 0.66 9.3 3.31 4 78 0.66 7.5 1.97 5 100 0.46 5.9 1.34 6 86.5 0.345 6.4 1.14 7 103.5 0.86 6.4 1.5 8 155.99 0.33 7.5 2.03 9 80.88 0.285 8.4 2.54 10 109.77 0.59 10.6 4.9 11 61.77 0.265 8.3 2.91 12 79.11 0.66 11.6 2.76 13 155.99 0.42 8.1 0.59 14 61.81 0.34 9.4 0.84 15 74.5 0.63 8.4 3.87 16 97 0.705 7.2 4.47 17 93.14 0.68 6.4 3.31 18 37.43 0.665 8.4 1.57 19 36.44 0.275 7.4 0.53 20 51 0.28 7.4 1.15 21 104 0.28 9.8 1.08 22 49 0.49 4.8 1.83 23 54.66 0.385 5.5 0.76 24 55.55 0.265 5 0.43 25 88.44 0.98 5 4.08 26 99.55 0.645 9.6 2.83 27 63.99 0.635 5.6 2.57 28 101.77 0.29 8.2 7.42 29 138.66 0.72 9.9 2.62 30 90.22 0.63 8.4 2 31 76.92 1.25 7.3 1.99 32 126.22 0.58 6.9 1.36 33 80.36 0.605 6.8 0.68 34 150.23 1.19 8.8 5.36 35 56.5 0.355 9.7 2.12

Page 34: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

34 

 

36 136 0.59 10.2 4.16 37 144.5 0.61 9.8 3.12 38 157.33 0.605 8.8 2.07 39 91.99 0.38 7.7 1.17 40 121.5 0.55 7.7 3.62 41 64.5 0.32 5.7 0.67 42 116 0.455 6.8 3.05 43 77.5 0.72 11.8 1.7 44 70.43 0.625 10 1.55 45 133.77 0.535 9.3 3.28 46 89.99 0.49 9.8 2.69 # R Script for Principal Component analysis attach(pca) names(pca) #To Remove first variable mydata=pca[,2:5] #perform principal component analysis output=princomp(mydata, cor=FALSE, scores=TRUE, covmat=NULL) print(output) #Summary results summary(output) #scree plot plot(output) #Loadings output$loadings #PC scores output$scores loadings(output, cuttoff=0.0001) print(output, digits=4, cuttoff=0.0001) biplot(output) detach(pca)  

D.2: Multivariate Analysis of variance Datafile: manova.txt blk trt y1 y2 1 1 722 808 1 2 640 742 1 3 640 910 1 4 936 1105 1 5 990 1185 1 6 995 1206 1 7 1145 1395

Page 35: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

35 

 

1 8 1190 1380 1 9 1032 1228 1 10 1250 1490 2 1 532 600 2 2 884 1025 2 3 800 945 2 4 765 900 2 5 835 1005 2 6 1070 1294 2 7 1140 1400 2 8 1040 1215 2 9 1145 1362 2 10 1180 1402 3 1 644 725 3 2 664 770 3 3 943 1120 3 4 893 1050 3 5 1071 1285 3 6 988 1195 3 7 880 1070 3 8 940 1090 3 9 1005 1195 3 10 860 1025 4 1 477 535 4 2 754 874 4 3 838 980 4 4 1052 1242 4 5 1037 1244 4 6 1170 1415 4 7 1075 1310 4 8 1150 1220 4 9 1035 1190 4 10 915 1025 # R Script for MANOVA attach(manova) names(manova) blk=factor(blk) trt=factor(trt) lm1=manova(cbind(y1,y2)~blk+trt) out=summary(lm1) out results=aov(lm1) results detach(manova)

Page 36: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

36 

 

E. Miscellaneous

E.1. Variance Components Estimation

Data:

 

 

 

 

 

 

 

 

 

#R-Script

library(varComp)

data<-read.csv(“insect_damage.csv”)

vcf = varComp(Y~., data, ~Variety)

summary(vcf)

E.2. GE interaction and Stability Analysis

Data: Yield data of various genotypes selected from various location available in the package agrocolae and name of the dataset is “plrv”. Partial data is given below-

Genotype Locality Rep WeightPlant WeightPlot Yield 1 102.18 Ayac 1 0.5100000 5.1000 18.8888889 2 104.22 Ayac 1 0.3450000 2.7600 12.7777778 3 121.31 Ayac 1 0.5425000 4.3400 20.0925926 4 141.28 Ayac 1 0.9888889 8.9000 36.6255144 5 157.26 Ayac 1 0.6250000 5.0000 23.1481481 6 163.9 Ayac 1 0.5120000 2.5600 18.9629630 7 221.19 Ayac 1 0.4960000 2.4800 18.3703704 8 233.11 Ayac 1 1.0100000 10.1000 37.4074074 9 235.6 Ayac 1 0.8250000 8.2500 30.5555556 10 241.2 Ayac 1 0.4880000 4.8800 18.0740741 11 255.7 Ayac 1 0.5800000 2.3200 21.4814815 12 314.12 Ayac 1 0.4100000 1.6400 15.1851852 13 317.6 Ayac 1 1.0057143 7.0400 37.2486772 14 319.20 Ayac 1 0.8644444 7.7800 32.0164609 15 320.16 Ayac 1 0.7066667 6.3600 26.1728395

Table 1: Insect Damage Data for Example

Page 37: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

37 

 

#R-Script:

 

  

 

 

E.3. Diallel Analysis: Data: Example was taken from the package named “plantbreeding” and the name of the data set is “fulldiallel”. Data is given below-

 

 

 

 

 

 

 

 

 

 

 

 

#R-Script:

library(plantbreeding) data(fulldial) out <-diallele1(dataframe = fulldial, male = "MALE", female = "FEMALE", progeny = "TRT", replication = "REP", yvar = "YIELD" ) print(out) out$anvout # analysis of variance out$anova.mod1 # analysis of variance for GCA and SCA effects out$components.model1 # model1 GCA, SCA and reciprocal components out$gca.effmat # GCA effects out$sca.effmat # SCA effect matrix heatmap(out$sca.effmat, labRow = rownames(out$sca.effmat), labCol = colnames(out$sca.effmat)) # heatmap plot of SCA matrix out$reciprocal.effmat # reciprocal effect matrix

library(agricolae) data(plrv) attach(plrv) model<- AMMI(Locality, Genotype, Rep, Yield) detach(plrv) # biplot PC2 vs PC1 plot(model) detach(plrv)

FAMILY TRT FAMQC MALE FEMALE REP YIELD 1 11 NA 1 1 1 104.86 1 11 NA 1 1 2 84.32 1 11 NA 1 1 3 76.92 1 11 NA 1 1 4 76.48 2 12 1 1 2 1 88.70 2 12 1 1 2 2 69.10 2 12 1 1 2 3 76.80 2 12 1 1 2 4 88.16 3 13 2 1 3 1 75.28 3 13 2 1 3 2 124.74 3 13 2 1 3 3 94.56 3 13 2 1 3 4 114.34 4 14 3 1 4 1 124.26 4 14 3 1 4 2 132.48 4 14 3 1 4 3 114.38 4 14 3 1 4 4 105.34 5 15 4 1 5 1 109.74 5 15 4 1 5 2 99.56 5 15 4 1 5 3 110.18 5 15 4 1 5 4 125.68

Page 38: R-Scripts for Statistical analysis using R-studio - IASRIiasri.res.in/sscnars/R_manual/R-Scripts for Statistical analysis... · 1 R-SCRIPTS FOR STATISTICAL ANALYSES USING R-STUDIO

38