contrasts and polynomial orthogonal

Muayyad

12/1/2021

#Data 1
dataa<-read.csv("D:\\Asisten MK STA512\\Praktikum 13b.csv",header=TRUE,sep = ";",
                colClasses =c("factor","factor","numeric")) 
str(dataa)
## 'data.frame':    12 obs. of  3 variables:
##  $ Perlakuan: Factor w/ 4 levels "a","b","c","d": 1 1 1 2 2 2 3 3 3 4 ...
##  $ Kelompok : Factor w/ 3 levels "1","2","3": 1 2 3 1 2 3 1 2 3 1 ...
##  $ Respon   : num  9.9 12.3 11.4 11.4 12.9 12.7 12.1 13.4 12.9 10.1 ...
model1<- aov(Respon ~ Perlakuan + Kelompok, data =dataa)
summary(model1)
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## Perlakuan    3  5.200   1.733   19.44 0.001713 ** 
## Kelompok     2  7.172   3.586   40.22 0.000335 ***
## Residuals    6  0.535   0.089                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Orthogonal Contrasts
levels(dataa$Perlakuan)
## [1] "a" "b" "c" "d"
kontras<-cbind(c(3,-1,-1,-1),c(0,1,-1,0),c(0,1,1,-2))
model11<- aov(Respon ~ Perlakuan + Kelompok, contrasts = list(Perlakuan=kontras), data =dataa)
summary.aov(model11,split=list(Perlakuan=list("a vs b,c,d"=1,"b vs d"=2,"b,c vs d, "=3)))
##                         Df Sum Sq Mean Sq F value   Pr(>F)    
## Perlakuan                3  5.200   1.733  19.439 0.001713 ** 
##   Perlakuan: a vs b,c,d  1  2.151   2.151  24.125 0.002679 ** 
##   Perlakuan: b vs d      1  0.327   0.327   3.664 0.104123    
##   Perlakuan: b,c vs d,   1  2.722   2.722  30.530 0.001480 ** 
## Kelompok                 2  7.172   3.586  40.215 0.000335 ***
## Residuals                6  0.535   0.089                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Polinomial Contrast
model1<- aov(Respon ~ Perlakuan + Kelompok, data =dataa)
contrasts(dataa$Perlakuan) <- contr.poly(4)
summary.aov(model1, split= list(Perlakuan=list("Linear" = 1, "Kuadratik" = 2, "Kubik"=3)))
##                        Df Sum Sq Mean Sq F value   Pr(>F)    
## Perlakuan               3  5.200   1.733  19.439 0.001713 ** 
##   Perlakuan: Linear     1  0.640   0.640   7.178 0.036580 *  
##   Perlakuan: Kuadratik  1  4.500   4.500  50.467 0.000391 ***
##   Perlakuan: Kubik      1  0.060   0.060   0.673 0.443404    
## Kelompok                2  7.172   3.586  40.215 0.000335 ***
## Residuals               6  0.535   0.089                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Data 2
library(readxl)
## Warning: package 'readxl' was built under R version 4.1.2
data2<-read_excel("D:/Asisten MK STA512/Praktikum 13.xlsx", col_names =T, sheet="Data")
data2
## # A tibble: 27 x 3
##    Perlakuan Kelompok Respon
##    <chr>        <dbl>  <dbl>
##  1 kontrol          1  10.2 
##  2 kontrol          2   9.26
##  3 kontrol          3  12.7 
##  4 K2P1             1  32.0 
##  5 K2P1             2  25.8 
##  6 K2P1             3  19.7 
##  7 K2P2             1  23.9 
##  8 K2P2             2  22.0 
##  9 K2P2             3  21.4 
## 10 K2P3             1  17.2 
## # ... with 17 more rows
str(data2)
## tibble [27 x 3] (S3: tbl_df/tbl/data.frame)
##  $ Perlakuan: chr [1:27] "kontrol" "kontrol" "kontrol" "K2P1" ...
##  $ Kelompok : num [1:27] 1 2 3 1 2 3 1 2 3 1 ...
##  $ Respon   : num [1:27] 10.19 9.26 12.73 32.02 25.76 ...
#View(data1)
respon=data2$Respon
kelompok=as.factor(data2$Kelompok)
perlakuan=as.factor(data2$Perlakuan)
Data1=data.frame(perlakuan,kelompok,respon)
plot(Data1$perlakuan,Data1$respon)

levels(Data1$perlakuan)
## [1] "K2P1"    "K2P2"    "K2P3"    "K2P4"    "K3P1"    "K3P2"    "K3P3"   
## [8] "K3P4"    "kontrol"
levels(Data1$kelompok)
## [1] "1" "2" "3"
model2<- aov(respon ~ perlakuan + kelompok, data =Data1)
summary(model2)
##             Df Sum Sq Mean Sq F value  Pr(>F)    
## perlakuan    8  586.0   73.25   8.297 0.00019 ***
## kelompok     2   39.2   19.61   2.221 0.14090    
## Residuals   16  141.3    8.83                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Orthogonal Contrasts
kontras2<-cbind(c(-1,-1,-1,-1,-1,-1,-1,-1,8),c(1,1,1,1,-1,-1,-1,-1,0),c(1,1,-1,-1,1,1,-1,-1,0))
model22<- aov(respon ~ perlakuan + kelompok, contrasts = list(perlakuan=kontras2), data =Data1)
summary.aov(model2,split=list(perlakuan=list("kontrol vs k2 k3"=1,"k2 vs k3"=2,"p1,p2 vs p3,p4"=3)))
##                               Df Sum Sq Mean Sq F value  Pr(>F)    
## perlakuan                      8  586.0   73.25   8.297 0.00019 ***
##   perlakuan: kontrol vs k2 k3  1  111.1  111.08  12.582 0.00268 ** 
##   perlakuan: k2 vs k3          1    8.7    8.70   0.985 0.33578    
##   perlakuan: p1,p2 vs p3,p4    1   44.3   44.27   5.015 0.03969 *  
## kelompok                       2   39.2   19.61   2.221 0.14090    
## Residuals                     16  141.3    8.83                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Polinomial Contrast
model2<- aov(respon ~ perlakuan + kelompok, data =Data1)
contrasts(Data1$perlakuan) <- contr.poly(9)
summary.aov(model2, split= list(perlakuan=list("Linear" = 1, "Kuadratik" = 2, "Kubik"=3, "Kuartik"=4)))
##                        Df Sum Sq Mean Sq F value  Pr(>F)    
## perlakuan               8  586.0   73.25   8.297 0.00019 ***
##   perlakuan: Linear     1  111.1  111.08  12.582 0.00268 ** 
##   perlakuan: Kuadratik  1    8.7    8.70   0.985 0.33578    
##   perlakuan: Kubik      1   44.3   44.27   5.015 0.03969 *  
##   perlakuan: Kuartik    1   20.5   20.46   2.317 0.14746    
## kelompok                2   39.2   19.61   2.221 0.14090    
## Residuals              16  141.3    8.83                    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Data 3
data11<-read.csv("D:\\Asisten MK STA512\\Data Responsi 13.csv",header=TRUE, sep=";",
                 colClasses =c("numeric","factor")) 
View(data11)
levels(data11$Suhu)
## [1] "10" "30" "50" "70" "90"
model13<- aov(Respon ~ Suhu, data = data11)
summary(model13)
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## Suhu         4   4451  1112.8   87.85 9.51e-08 ***
## Residuals   10    127    12.7                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Orthogonal Contrasts
kontras3<-cbind(c(2,2,2,-3,-3),c(2,-1,-1,0,0),c(0,1,-1,0,0),c(0,0,0,1,-1))
model33<- aov(Respon ~ Suhu, contrasts = list(Suhu=kontras3), data =data11)
summary.aov(model33,split=list(Suhu=list("Sedang vs Tinggi"=1,"10 vs 30"=2,"30 vs 50"=3,"70 vs 90"=4)))
##                          Df Sum Sq Mean Sq F value   Pr(>F)    
## Suhu                      4   4451  1112.8  87.850 9.51e-08 ***
##   Suhu: Sedang vs Tinggi  1   1832  1831.5 144.593 2.87e-07 ***
##   Suhu: 10 vs 30          1   2473  2473.4 195.268 6.89e-08 ***
##   Suhu: 30 vs 50          1    140   140.2  11.066  0.00766 ** 
##   Suhu: 70 vs 90          1      6     6.0   0.474  0.50695    
## Residuals                10    127    12.7                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Polinomial Contrast
model13<- aov(Respon ~ Suhu, data = data11)
contrasts(data11$Suhu) <- contr.poly(5)
summary.aov(model33, split= list(Suhu=list("Linear" = 1, "Kuadratik" = 2, "Kubik"=3, "Kuartik"=4)))
##                   Df Sum Sq Mean Sq F value   Pr(>F)    
## Suhu               4   4451  1112.8  87.850 9.51e-08 ***
##   Suhu: Linear     1   1832  1831.5 144.593 2.87e-07 ***
##   Suhu: Kuadratik  1   2473  2473.4 195.268 6.89e-08 ***
##   Suhu: Kubik      1    140   140.2  11.066  0.00766 ** 
##   Suhu: Kuartik    1      6     6.0   0.474  0.50695    
## Residuals         10    127    12.7                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
dataa<-read.csv("D:\\Asisten MK STA512\\Data Responsi 13.csv",header=TRUE, sep=";")
plot(dataa$Suhu,dataa$Respon)

#modelregresi
m1<-lm(Respon ~ Suhu , data=dataa)
summary(m1)
## 
## Call:
## lm(formula = Respon ~ Suhu, data = dataa)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -10.533  -7.467  -2.533   7.467  14.733 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 52.70000    4.67958   11.26 4.46e-08 ***
## Suhu        -0.54333    0.08146   -6.67 1.54e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 8.924 on 13 degrees of freedom
## Multiple R-squared:  0.7739, Adjusted R-squared:  0.7565 
## F-statistic: 44.49 on 1 and 13 DF,  p-value: 1.54e-05
suhu2=dataa$Suhu^2
m2<-lm(Respon ~ Suhu + suhu2, data=dataa)
summary(m2)
## 
## Call:
## lm(formula = Respon ~ Suhu + suhu2, data = dataa)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.0190 -1.6667 -0.7714  0.8429  7.7143 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 71.319048   3.668929  19.439 1.94e-10 ***
## Suhu        -1.638571   0.173210  -9.460 6.50e-07 ***
## suhu2        0.010952   0.001686   6.498 2.95e-05 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.369 on 12 degrees of freedom
## Multiple R-squared:   0.95,  Adjusted R-squared:  0.9416 
## F-statistic: 113.9 on 2 and 12 DF,  p-value: 1.571e-08
suhu3=dataa$Suhu^3
m3<-lm(Respon ~ Suhu + suhu2 + suhu3, data=dataa)
summary(m3)
## 
## Call:
## lm(formula = Respon ~ Suhu + suhu2 + suhu3, data = dataa)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.8190 -2.4857 -0.7952  2.3595  6.2286 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  8.181e+01  4.812e+00  17.001 3.03e-09 ***
## Suhu        -2.769e+00  4.328e-01  -6.397 5.10e-05 ***
## suhu2        3.856e-02  1.010e-02   3.817  0.00286 ** 
## suhu3       -1.840e-04  6.674e-05  -2.757  0.01864 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.509 on 11 degrees of freedom
## Multiple R-squared:  0.9704, Adjusted R-squared:  0.9623 
## F-statistic: 120.2 on 3 and 11 DF,  p-value: 1.088e-08
suhu4=dataa$Suhu^4
m4<-lm(Respon ~ Suhu + suhu2 + suhu3 + suhu4, data=dataa)
summary(m4)
## 
## Call:
## lm(formula = Respon ~ Suhu + suhu2 + suhu3 + suhu4, data = dataa)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -3.000 -2.000 -1.667  2.667  5.000 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  8.984e+01  1.079e+01   8.323 8.32e-06 ***
## Suhu        -3.974e+00  1.511e+00  -2.630   0.0251 *  
## suhu2        8.793e-02  6.010e-02   1.463   0.1742    
## suhu3       -9.306e-04  8.980e-04  -1.036   0.3245    
## suhu4        3.733e-06  4.477e-06   0.834   0.4239    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.559 on 10 degrees of freedom
## Multiple R-squared:  0.9723, Adjusted R-squared:  0.9613 
## F-statistic: 87.85 on 4 and 10 DF,  p-value: 9.508e-08
dataa<-read.csv("D:\\Asisten MK STA512\\Praktikum-13.csv",header=TRUE,sep = ";",
                colClasses =c("factor","numeric")) 
str(dataa)
## 'data.frame':    15 obs. of  2 variables:
##  $ Perlakuan: Factor w/ 5 levels "0","16","32",..: 1 1 1 2 2 2 3 3 3 4 ...
##  $ Respon   : num  96 98 94 92 88 90 92 94 84 74 ...
model1<- aov(Respon ~ Perlakuan , data =dataa)
summary(model1)
##             Df Sum Sq Mean Sq F value   Pr(>F)    
## Perlakuan    4   4025  1006.3   94.34 6.73e-08 ***
## Residuals   10    107    10.7                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Orthogonal Contrasts
model1<- aov(Respon ~ Perlakuan,  data =dataa)
kontras3<-cbind(c(2,-1,0,-1,2),c(2,-1,-2,-1,2),c(-1,2,0,-2,1),c(1,-4,6,-4,1))
model33<- aov(Respon ~ Perlakuan, contrasts = list(Perlakuan=kontras3), data =dataa)
summary.aov(model1,split=list(Perlakuan=list("Linear" = 1, "Kuadratik" = 2, "Kubik"=3, "kuartik"=4)))
##                        Df Sum Sq Mean Sq F value   Pr(>F)    
## Perlakuan               4   4025  1006.3  94.337 6.73e-08 ***
##   Perlakuan: Linear     1    385   385.1  36.100 0.000131 ***
##   Perlakuan: Kuadratik  1    642   641.8  60.167 1.54e-05 ***
##   Perlakuan: Kubik      1      6     5.6   0.521 0.487018    
##   Perlakuan: kuartik    1   2993  2992.7 280.562 1.21e-08 ***
## Residuals              10    107    10.7                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Polinomial Contrast
model1<- aov(Respon ~ Perlakuan,  data =dataa)
contrasts(dataa$Perlakuan) <- contr.poly(5)
summary.aov(model1, split= list(Perlakuan=list("Linear" = 1, "Kuadratik" = 2, "Kubik"=3, "kuartik"=4)))
##                        Df Sum Sq Mean Sq F value   Pr(>F)    
## Perlakuan               4   4025  1006.3  94.337 6.73e-08 ***
##   Perlakuan: Linear     1    385   385.1  36.100 0.000131 ***
##   Perlakuan: Kuadratik  1    642   641.8  60.167 1.54e-05 ***
##   Perlakuan: Kubik      1      6     5.6   0.521 0.487018    
##   Perlakuan: kuartik    1   2993  2992.7 280.562 1.21e-08 ***
## Residuals              10    107    10.7                     
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
dataa<-read.csv("D:\\Asisten MK STA512\\Praktikum-13.csv",header=TRUE,sep = ";")

#modelregresi
plot(dataa$Perlakuan,dataa$Respon)
m1<-lm(Respon ~ Perlakuan , data=dataa)
summary(m1)
## 
## Call:
## lm(formula = Respon ~ Perlakuan, data = dataa)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -8.400 -4.867 -1.133  4.500 14.133 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 101.33333    3.22395  31.431 1.19e-13 ***
## Perlakuan    -0.67083    0.08226  -8.155 1.81e-06 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 7.209 on 13 degrees of freedom
## Multiple R-squared:  0.8365, Adjusted R-squared:  0.8239 
## F-statistic:  66.5 on 1 and 13 DF,  p-value: 1.811e-06
Perlakuan2=dataa$Perlakuan^2
m2<-lm(Respon ~ Perlakuan  + Perlakuan2, data=dataa)
summary(m2)
## 
## Call:
## lm(formula = Respon ~ Perlakuan + Perlakuan2, data = dataa)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.9810 -2.3048 -0.5714  1.9238  7.3714 
## 
## Coefficients:
##              Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 94.571429   2.193180  43.121 1.57e-14 ***
## Perlakuan    0.174405   0.162375   1.074 0.303906    
## Perlakuan2  -0.013207   0.002433  -5.428 0.000153 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 4.036 on 12 degrees of freedom
## Multiple R-squared:  0.9527, Adjusted R-squared:  0.9448 
## F-statistic: 120.8 on 2 and 12 DF,  p-value: 1.122e-08
Perlakuan3=dataa$Perlakuan^3
m3<-lm(Respon ~ Perlakuan  + Perlakuan2 + Perlakuan3, data=dataa)
summary(m3)
## 
## Call:
## lm(formula = Respon ~ Perlakuan + Perlakuan2 + Perlakuan3, data = dataa)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -6.2476 -1.8429 -0.2476  1.5619  7.3714 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 95.4380952  2.2730330  41.987  1.7e-13 ***
## Perlakuan   -0.2137897  0.3613978  -0.592    0.566    
## Perlakuan2   0.0037202  0.0143409   0.259    0.800    
## Perlakuan3  -0.0001763  0.0001473  -1.197    0.256    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.965 on 11 degrees of freedom
## Multiple R-squared:  0.9581, Adjusted R-squared:  0.9467 
## F-statistic: 83.92 on 3 and 11 DF,  p-value: 7.292e-08
Perlakuan4=dataa$Perlakuan ^4
m4<-lm(Respon ~ Perlakuan  + Perlakuan2 + Perlakuan3 + Perlakuan4, data=dataa)
summary(m4)
## 
## Call:
## lm(formula = Respon ~ Perlakuan + Perlakuan2 + Perlakuan3 + Perlakuan4, 
##     data = dataa)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -6.000 -1.667  0.000  2.000  4.000 
## 
## Coefficients:
##               Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  9.600e+01  1.886e+00  50.912 2.07e-13 ***
## Perlakuan   -1.677e+00  6.581e-01  -2.548   0.0289 *  
## Perlakuan2   1.290e-01  5.162e-02   2.499   0.0315 *  
## Perlakuan3  -3.377e-03  1.290e-03  -2.619   0.0256 *  
## Perlakuan4   2.501e-05  1.003e-05   2.493   0.0318 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.266 on 10 degrees of freedom
## Multiple R-squared:  0.9742, Adjusted R-squared:  0.9639 
## F-statistic: 94.34 on 4 and 10 DF,  p-value: 6.733e-08
plot(dataa$Perlakuan,dataa$Respon)

library(ggplot2)

ggplot(dataa,aes(x=Perlakuan, y=Respon)) + 
  geom_point(alpha=0.95, color="black") +
  stat_smooth(method = "lm", 
              formula = y~poly(x,4,raw=T), 
              lty = 1, col = "blue",se = F)+
  stat_smooth(method = "lm", 
              formula = y~poly(x,2,raw=T), 
              lty = 1, col = "red",se = F)+
  stat_smooth(method = "lm", 
              formula = y~poly(x,1,raw=T), 
              lty = 1, col = "orange",se = F)+
  theme_bw()