library(readxl)
data1<-read.csv("C:/Users/ASUS/Documents/UNY/SEM 2/ANALISIS REGRESI/Tugas AnReg/soal1.csv")
data1<- data.frame(data1)
head(data1)
## Source DF SS MS F
## 1 Regression 1 500000 NA NA
## 2 Error 35 300000 NA NA
## 3 Total 36 800000 NA NA
MSR <- data1[1,3]/data1[1,2]
MSR
## [1] 5e+05
MSE <- data1[2,3]/data1[2,2]
MSE
## [1] 8571.429
F_value <- sum(MSR/MSE)
F_value
## [1] 58.33333
source <- c("Regression", "Error", "Total")
DF <- c(1,35,36)
SS = c(500000,300000,800000)
MS <- c(MSR,MSE, " ")
F <- c(F_value, " ", " ")
data1 <- data.frame(source, DF, SS, MS, F)
data1
## source DF SS MS F
## 1 Regression 1 5e+05 5e+05 58.3333333333333
## 2 Error 35 3e+05 8571.42857142857
## 3 Total 36 8e+05
library(readxl)
data2<-read.csv("C:/Users/ASUS/Documents/UNY/SEM 2/ANALISIS REGRESI/Tugas AnReg/soal2.csv")
data2<-data.frame(data2)
head(data2)
## Source DF SS MS F p
## 1 Regression 1 182220 182220 135.07 0
## 2 Error 98 132213 1349 NA NA
## 3 Total 99 314433 NA NA NA
#DF Total = n-1
sample_size <- sum((data2[3,2]+1))
sample_size
## [1] 100
# The sample mean selling price
# y = the selling price (in thousands of dollars)
# x = the size of house (in thousands square feet)
# yhat equal when x = xbar, yhat = y
x <- 1.53
yhat <- 9.2 + 77*x
yhat
## [1] 127.01
library(readxl)
data<-read.csv("C:/Users/ASUS/Documents/UNY/SEM 2/ANALISIS REGRESI/Tugas AnReg/data_tugas3.csv")
head(data)
## Height Weight
## 1 1.47 52.21
## 2 1.50 53.12
## 3 1.52 54.48
## 4 1.55 55.84
## 5 1.57 57.20
## 6 1.60 58.57
plot(data$Height, data$Weight,xlab="Height",ylab="Weight",pch=16)
data$xdif <- data$Height-mean(data$Height)
data$ydif <- data$Weight-mean(data$Weight)
data$crp <- data$xdif*data$ydif
data$xsq <- data$xdif^2
#estimator b0 dan b1
b1 <- sum(data$crp)/sum(data$xsq)
b1
## [1] 61.27219
#Parameter Estimates
b0 <- mean(data$Weight) - b1 * mean(data$Height)
b0
## [1] -39.06196
model1<-lm(Weight~Height,data=data)
summary(model1)
##
## Call:
## lm(formula = Weight ~ Height, data = data)
##
## Residuals:
## Min 1Q Median 3Q Max
## -0.88171 -0.64484 -0.06993 0.34095 1.39385
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -39.062 2.938 -13.29 6.05e-09 ***
## Height 61.272 1.776 34.50 3.60e-14 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.7591 on 13 degrees of freedom
## Multiple R-squared: 0.9892, Adjusted R-squared: 0.9884
## F-statistic: 1190 on 1 and 13 DF, p-value: 3.604e-14
plot(data$Height, data$Weight,xlab="Height",ylab="Weight",pch=16)
abline(model1)
JKG<-sum((data$Weight-model1$fitted.values)^2)
JKG
## [1] 7.490558
JKR<-sum(((model1$fitted.values-mean(data$Weight))^2))
JKR
## [1] 685.8821
JKT<-sum((data$Weight-mean(data$Weight))^2)
JKT
## [1] 693.3726
#koefisien determinasi
r_sqr<-(JKT-JKG)/JKT
r_sqr
## [1] 0.9891969
#koefisien korelasi
sqrt(r_sqr)
## [1] 0.9945838
anova(model1)
## Analysis of Variance Table
##
## Response: Weight
## Df Sum Sq Mean Sq F value Pr(>F)
## Height 1 685.88 685.88 1190.4 3.604e-14 ***
## Residuals 13 7.49 0.58
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#koefisien korelasi cara 1
x1_x1bar<-data$Height-mean(data$Height)
x2_x2bar<-data$Weight-mean(data$Weight)
A<-sum(x1_x1bar*x2_x2bar)
varx1<-sum((x1_x1bar)^2)
varx2<-sum((x2_x2bar)^2)
B<-sqrt(varx1*varx2)
corr<-A/B
corr
## [1] 0.9945838
#koefisien korelasi cara 2
cor(data$Height,data$Weight)
## [1] 0.9945838
#Pearson Correlation test, alpha 1%
cor.test( ~ Height + Weight, data=data, method = "pearson", continuity = FALSE, conf.level = 0.99)
##
## Pearson's product-moment correlation
##
## data: Height and Weight
## t = 34.502, df = 13, p-value = 3.604e-14
## alternative hypothesis: true correlation is not equal to 0
## 99 percent confidence interval:
## 0.9762562 0.9987733
## sample estimates:
## cor
## 0.9945838
H0 ditolak jika p-value < tingkat signifikansi (alpha)
Dengan nilai koefisien korelasi sebesar 0.9945838, menunjukkan hubungan positif yang sangat kuat antara tinggi dan berat badan.
Hasil uji signifikansi menunjukkan bahwa korelasi ini secara statistik signifikan dengan p-value sebesar 3.604e-14. Dimana p-value < tingkat signifikansi (0.01), Sehingga :
hipotesis nol(H0) dapat ditolak.
Dengan Demikian, dapat disimpulkan bahwa terdapat korelasi antara tinggi badan dan berat badan.
library(readxl)
data4<-read.csv("C:/Users/ASUS/Documents/UNY/SEM 2/ANALISIS REGRESI/Tugas AnReg/data4.csv")
head(data4)
## Umur Massa.Otot
## 1 71 82
## 2 64 91
## 3 43 100
## 4 67 68
## 5 56 87
## 6 73 73
plot(data4$Umur, data4$Massa.Otot,xlab="Umur",ylab="Massa.Otot",pch=16)
data4$xdif <- data4$Umur-mean(data4$Umur)
data4$ydif <- data4$Massa.Otot-mean(data4$Massa.Otot)
data4$crp <- data4$xdif*data4$ydif
data4$xsq <- data4$xdif^2
#estimator b0 dan b1
b1 <- sum(data4$crp)/sum(data4$xsq)
b1
## [1] -1.023589
#Parameter Estimates
b0 <- mean(data4$Massa.Otot) - b1 * mean(data4$Umur)
b0
## [1] 148.0507
model1<-lm(Umur~Massa.Otot,data=data4)
summary(model1)
##
## Call:
## lm(formula = Umur ~ Massa.Otot, data = data4)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.193 -6.191 1.617 3.946 11.470
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 117.5935 10.6411 11.051 2.67e-08 ***
## Massa.Otot -0.6632 0.1219 -5.439 8.72e-05 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 6.716 on 14 degrees of freedom
## Multiple R-squared: 0.6788, Adjusted R-squared: 0.6559
## F-statistic: 29.59 on 1 and 14 DF, p-value: 8.721e-05
plot(data4$Umur, data4$Massa.Otot,xlab="Umur",ylab="Massa.Otot",pch=16)
abline(model1)
JKG<-sum((data4$Massa.Otot-model1$fitted.values)^2)
JKG
## [1] 19002.54
JKR<-sum(((model1$fitted.values-mean(data4$Massa.Otot))^2))
JKR
## [1] 11943.48
JKT<-sum((data4$Massa.Otot-mean(data4$Massa.Otot))^2)
JKT
## [1] 3034.438
#koefisien determinasi
r_sqr<-(JKT-JKG)/JKT
r_sqr
## [1] -5.262295
anova(model1)
## Analysis of Variance Table
##
## Response: Umur
## Df Sum Sq Mean Sq F value Pr(>F)
## Massa.Otot 1 1334.48 1334.5 29.587 8.721e-05 ***
## Residuals 14 631.46 45.1
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#koefisien korelasi cara 1
x1_x1bar<-data4$Umur-mean(data4$Umur)
x2_x2bar<-data4$Massa.Otot-mean(data4$Massa.Otot)
A<-sum(x1_x1bar*x2_x2bar)
varx1<-sum((x1_x1bar)^2)
varx2<-sum((x2_x2bar)^2)
B<-sqrt(varx1*varx2)
corr<-A/B
corr
## [1] -0.8238943
#koefisien korelasi cara 2
cor(data4$Umur,data4$Massa.Otot)
## [1] -0.8238943
#Pearson Correlation test, alpha 1%
cor.test( ~ Umur + Massa.Otot, data=data4, method = "pearson", continuity = FALSE, conf.level = 0.99)
##
## Pearson's product-moment correlation
##
## data: Umur and Massa.Otot
## t = -5.4394, df = 14, p-value = 8.721e-05
## alternative hypothesis: true correlation is not equal to 0
## 99 percent confidence interval:
## -0.9547784 -0.4255220
## sample estimates:
## cor
## -0.8238943
H0 ditolak jika p-value < tingkat signifikansi (alpha)
Dari perhitungan R didapat p-value = (8.721e-05), dimana hasil p-value < tingkat signifikansi yang umum digunakan (0.05). Sehingga :
hipotesis nol(H0) dapat ditolak.
Dengan Demikian, dapat disimpulkan bahwa terdapat korelasi antara Umur dan massa otot.