This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

regresi linier sederhana

#memanggil package

library(readxl)

#input data
datprom=read_excel("E:\\Praktikum 3.xlsx", sheet="Sheet1")
datprom
## # A tibble: 15 x 2
##    `Promosi (kali/bulan) (X)` `Penjualan (unit/hari)          (Y)`
##                         <dbl>                                <dbl>
##  1                         25                                  100
##  2                         27                                  105
##  3                         29                                  108
##  4                         30                                  122
##  5                         35                                  120
##  6                         50                                  145
##  7                         55                                  143
##  8                         60                                  150
##  9                         63                                  148
## 10                         65                                  157
## 11                         70                                  161
## 12                         71                                  175
## 13                         73                                  174
## 14                         75                                  176
## 15                         80                                  185
str(datprom)
## tibble [15 x 2] (S3: tbl_df/tbl/data.frame)
##  $ Promosi (kali/bulan) (X)          : num [1:15] 25 27 29 30 35 50 55 60 63 65 ...
##  $ Penjualan (unit/hari)          (Y): num [1:15] 100 105 108 122 120 145 143 150 148 157 ...
#mengganti nama kolom
colnames(datprom)[1]="X"
colnames(datprom)[2]="Y"

#membangun model regresi
regresi=lm(Y~X,data=datprom) #menggunkan fungsi lm (linier model)
regresi
## 
## Call:
## lm(formula = Y ~ X, data = datprom)
## 
## Coefficients:
## (Intercept)            X  
##      69.609        1.392
summary(regresi)
## 
## Call:
## lm(formula = Y ~ X, data = datprom)
## 
## Residuals:
##    Min     1Q Median     3Q    Max 
## -9.315 -3.158 -1.982  3.391 10.626 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 69.60912    4.31285   16.14 5.57e-10 ***
## X            1.39216    0.07551   18.44 1.06e-10 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 5.554 on 13 degrees of freedom
## Multiple R-squared:  0.9632, Adjusted R-squared:  0.9603 
## F-statistic: 339.9 on 1 and 13 DF,  p-value: 1.058e-10
#plot regresi
plot(datprom$X,datprom$Y,main="Plot data Promosi (X) dan Penjualan (Y)",
     xlab="Promosi (X)",ylab="Penjualan (Y)")
abline(regresi,col="red")

#selang kepercayaan bagi parameter
confint(regresi)
##                 2.5 %    97.5 %
## (Intercept) 60.291787 78.926462
## X            1.229028  1.555287
confint(regresi, level = 0.80)
##                  10 %      90 %
## (Intercept) 63.786043 75.432205
## X            1.290206  1.494109
#derajat bebas
n=nrow(datprom);n
## [1] 15
db_r=1
db_s=n-2;db_s
## [1] 13
y_dugaan=regresi$fitted.values;y_dugaan
##        1        2        3        4        5        6        7        8 
## 104.4131 107.1974 109.9817 111.3738 118.3346 139.2170 146.1778 153.1386 
##        9       10       11       12       13       14       15 
## 157.3150 160.0994 167.0601 168.4523 171.2366 174.0209 180.9817
rataan_Y=mean(datprom$Y);rataan_Y
## [1] 144.6
# Manual ANOVA
JKR=sum((y_dugaan-rataan_Y)^2)
JKR
## [1] 10484.62
JKS=sum((datprom$Y-y_dugaan)^2)
JKS
## [1] 400.9846
JKT=JKR+JKS
JKT
## [1] 10885.6
KTR=JKR
KTR
## [1] 10484.62
KTS=JKS/db_s
KTS
## [1] 30.84497
tabel_anova=data.frame(SK=c("Regresi","Sisaan","Total"),
                       db=c(db_r,db_s,db_r+db_s),
                       JK=c(JKR,JKS,JKT),
                       KT=c(KTR,KTS,""))
tabel_anova
##        SK db         JK               KT
## 1 Regresi  1 10484.6154 10484.6154043329
## 2  Sisaan 13   400.9846 30.8449688974669
## 3   Total 14 10885.6000
#melihat anova/ tabel sidik ragam
anova(regresi)
## Analysis of Variance Table
## 
## Response: Y
##           Df Sum Sq Mean Sq F value    Pr(>F)    
## X          1  10485 10484.6  339.91 1.058e-10 ***
## Residuals 13    401    30.8                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#Koefisien detterminasi atau R^2
R2<-JKR/JKT
R2
## [1] 0.9631638
#prediksi jika X*=45
prediksi<- regresi$coefficients[[1]]+regresi$coefficients[[2]]*45
prediksi
## [1] 132.2562

regresi melalui titik origin

#input data
tanah=read_excel("E:\\Praktikum 4.xlsx", sheet="Sheet4")
tanah
## # A tibble: 15 x 2
##        X     Y
##    <dbl> <dbl>
##  1    29  27.8
##  2     6   5.7
##  3     8   9.3
##  4    17  15.8
##  5     2   3.1
##  6    23  24.8
##  7    11  11.5
##  8    30  34.7
##  9    17  18  
## 10    14  16.5
## 11    19  19.3
## 12     4   5.6
## 13    24  23.5
## 14     1   1.5
## 15    12  12.8
plot(tanah)

#regresi dengan intersep
reg1=lm(Y~X,data=tanah)
summary(reg1)
## 
## Call:
## lm(formula = Y ~ X, data = tanah)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.3264 -0.8277 -0.0201  0.6771  3.6185 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept)  0.63924    0.75684   0.845    0.414    
## X            1.01508    0.04436  22.885  6.9e-12 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.554 on 13 degrees of freedom
## Multiple R-squared:  0.9758, Adjusted R-squared:  0.9739 
## F-statistic: 523.7 on 1 and 13 DF,  p-value: 6.896e-12
anova(reg1)
## Analysis of Variance Table
## 
## Response: Y
##           Df Sum Sq Mean Sq F value    Pr(>F)    
## X          1 1265.0 1265.03   523.7 6.896e-12 ***
## Residuals 13   31.4    2.42                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#plot regresi model dengan intersep
plot(tanah$X,tanah$Y,main="Plot data Luas Tanah (X) dan Harga (Y)",
     xlab="Luas Tanah (X)",ylab="Harga (Y)")
abline(reg1,col="red")


#regresi tanpa instersep
reg2=lm(Y~X-1,data=tanah)
summary(reg2)
## 
## Call:
## lm(formula = Y ~ X - 1, data = tanah)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.6084 -0.5855  0.2379  0.9658  3.3048 
## 
## Coefficients:
##   Estimate Std. Error t value Pr(>|t|)    
## X  1.04684    0.02328   44.97   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.538 on 14 degrees of freedom
## Multiple R-squared:  0.9931, Adjusted R-squared:  0.9926 
## F-statistic:  2023 on 1 and 14 DF,  p-value: < 2.2e-16
anova(reg2)
## Analysis of Variance Table
## 
## Response: Y
##           Df Sum Sq Mean Sq F value    Pr(>F)    
## X          1 4785.7  4785.7  2022.6 < 2.2e-16 ***
## Residuals 14   33.1     2.4                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
confint(reg2)
##       2.5 %   97.5 %
## X 0.9969159 1.096764
#plot regresi model tanpa intersep
abline(reg2,col="blue")

regresi Linier Berganda

#input data
data=read_excel("E:\\Praktikum-5.xlsx",sheet="Sheet1")
data
## # A tibble: 25 x 4
##       NO     y    x1    x2
##    <dbl> <dbl> <dbl> <dbl>
##  1     1  16.7     7   560
##  2     2  11.5     3   220
##  3     3  12.0     3   340
##  4     4  14.9     4    80
##  5     5  13.8     6   150
##  6     6  18.1     7   330
##  7     7   8       2   110
##  8     8  17.8     7   210
##  9     9  79.2    30  1460
## 10    10  21.5     5   605
## # ... with 15 more rows
plot(data) 

#melihat plot
#x1 vs y
plot(data$x1,data$y,main="Jumlah Minuman (X1) dan Lama Pengantaran (Y)",
     xlab="Jumlah Minuman (X1)",ylab="Lama Pengantaran (Y)")

#x2 vs y
plot(data$x2,data$y,main="Jarak Tempuh (X2) dan Lama Pengantaran (Y)",
     xlab="Jarak Tempuh (X2)",ylab="Lama Pengantaran (Y)")

#membuat model regresi
reg_ganda=lm(y~x1+x2,data=data)
summary(reg_ganda)
## 
## Call:
## lm(formula = y ~ x1 + x2, data = data)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -5.7880 -0.6629  0.4364  1.1566  7.4197 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 2.341231   1.096730   2.135 0.044170 *  
## x1          1.615907   0.170735   9.464 3.25e-09 ***
## x2          0.014385   0.003613   3.981 0.000631 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 3.259 on 22 degrees of freedom
## Multiple R-squared:  0.9596, Adjusted R-squared:  0.9559 
## F-statistic: 261.2 on 2 and 22 DF,  p-value: 4.687e-16

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.