Ekonometrik
UTS
| Kontak | : \(\downarrow\) |
| yosia.yosia@student.matanauniversity.ac.id | |
| yyosia | |
| RPubs | https://rpubs.com/yosia/ |
1
Apa perbedaan regressi Linear Sederhana dan Berganda, jelaskan dengan contoh!
Regresi Linier Sederhana dan Berganda/Multiple
| Linear | Multiple |
|---|---|
| terdapat 1 variable Y dependen dan 1 variable X independen | terdapat 1 variable Y dependen dan lebih dari satu variabel X atau variable Independen |
| 1 koefisien regresi | 1 koefisien regresi untuk setiap variabel independen |
| \(r^2\) = proporsi variasi variable dependen Y yang dapat diprediksi dari X | \(R^2\) = proporsi variasi variable dependen Y diprediksi oleh himpunan variabel independen (X) |
| \(Y=a+bx\) | \(Y=b_0+b_1x_1+b_2x_2+...+b_nx_n\) |
Contoh regresi Linear Sederhana Dalam penjualan mobil telah dikumpulkan data, dimana X(prediktor) adalah usia mobil dan Y(respon) adalah Harga Mobil dilihat dari contoh diatas bahwa hanya ada 1 variable dalam kondisi ini. jadi kita ingin melihat apakah usia mobil mempengaruhi harga mobil pada saat dijual.
Contoh regresi Linear Berganda Seorang mahasiswa ingin meneliti di sekolah apakah Usia dan Tinggi mempengaruhi berat badan seseorang. diketahui bahwa ada 2 variable prediktor yang diduga mempengaruhi variable respon (berat badan).
2
Lakukan analisis regresi linear sederhana dalam ilmu ekonometrik!
memasukan dataset
df = read.csv("salary_data.csv", sep=",")
library(DT)
datatable(df)Setelah memasukan data kita membuat plot
x = df$YearsExperience
y = df$Salary
plot(x,y,
ylim=c(0, max(y)),
xlim=c(0, max(x)),
xlab="YearsExperience",
ylab="Salary",
type = "p")Simple regression
mod1 <- lm( y~x , data = df)
summary(mod1)##
## Call:
## lm(formula = y ~ x, data = df)
##
## Residuals:
## Min 1Q Median 3Q Max
## -7958.0 -4088.5 -459.9 3372.6 11448.0
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 25792.2 2273.1 11.35 5.51e-12 ***
## x 9450.0 378.8 24.95 < 2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 5788 on 28 degrees of freedom
## Multiple R-squared: 0.957, Adjusted R-squared: 0.9554
## F-statistic: 622.5 on 1 and 28 DF, p-value: < 2.2e-16
dilihat dari t value bahwa
b1 <- coef(mod1)[[1]]
b2 <- coef(mod1)[[2]]
print(data.frame (b1,b2))## b1 b2
## 1 25792.2 9449.962
maka kita mendapatkan persamaan regresi nya adalah \(\hat{y}=25792.2 +9449.962X\) dari hasil estimasi yang diperoleh dapat disimpulkan bahwa setiap bekerja selama 1 tahun maka salarynya bertambah $9449.962
plot(x,y,
ylim=c(0, max(y)),
xlim=c(0, max(x)),
xlab="YearsExperience",
ylab="Salary",
type = "p")
abline(b1,b2,col="red")confint(mod1)## 2.5 % 97.5 %
## (Intercept) 21136.061 30448.34
## x 8674.119 10225.81
anova(mod1)Memprediksi nilai setelah regresi
Y1_topi <- fitted(mod1)
summary(Y1_topi)## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 36187 56032 70207 76003 98557 125017
plot(Y1_topi ~ x)Regresi Residual
elhat <- resid(mod1)
summary(elhat)## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -7958.0 -4088.5 -459.9 0.0 3372.6 11448.0
plot(elhat ~ x)Estimasi nilai Cov dan Var
(varb1 <- vcov(mod1)[1,1])## [1] 5166772
(varb2 <- vcov(mod1)[2,2])## [1] 143455
(varb1b2 <- vcov(mod1)[1,2])## [1] -762224.4
3
Carilah contoh penerapan analisis regresi linear berganda dalam ilmu ekonometrik!
masukan dataset
df1<-read.csv("C:/Users/House Of Grace/OneDrive/Documents/data/ekonometrik/mlr09.csv",
sep=";")
library(DT)
datatable(df1)pada dataset diatas diketahui bahwa
- X1 = score on exam #1
- X2 = score on exam #2
- X3 = score on exam #3
- Y = score on final exam
data diatas diambil dari “Test Scores for General Psychology” Jadi ada 3 faktor/prediktor yang diduga mempengaruhi score on final exam, dalam hal ini kita akan melihat apakah variable-variable independen mempengaruhi nilai akhirnya
df1 %>%
select(EXAM1, EXAM2, EXAM3, FINAL) %>%
stargazer(type = "text")##
## ===========================================
## Statistic N Mean St. Dev. Min Max
## -------------------------------------------
## EXAM1 54 40.089 37.430 5.700 96.000
## EXAM2 54 147.407 68.945 46 263
## EXAM3 54 37.960 41.548 0.291 100.000
## FINAL 54 80.544 78.147 2.800 196.000
## -------------------------------------------
Multiple regression
model_multiple <- lm(FINAL~EXAM1+EXAM2+EXAM3 , data=df1)
summary(model_multiple)##
## Call:
## lm(formula = FINAL ~ EXAM1 + EXAM2 + EXAM3, data = df1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -9.1953 -2.3145 -0.0636 2.3912 13.6201
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.265486 4.008794 0.815 0.419
## EXAM1 0.728777 0.166891 4.367 6.34e-05 ***
## EXAM2 0.007956 0.019620 0.405 0.687
## EXAM3 1.235248 0.158368 7.800 3.40e-10 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.473 on 50 degrees of freedom
## Multiple R-squared: 0.9969, Adjusted R-squared: 0.9967
## F-statistic: 5377 on 3 and 50 DF, p-value: < 2.2e-16
library(effects)
effX1 <- effect("EXAM1",model_multiple)
summary(effX1)##
## EXAM1 effect
## EXAM1
## 6 30 50 70 100
## 55.70124 73.19189 87.76744 102.34298 124.20629
##
## Lower 95 Percent Confidence Limits
## EXAM1
## 6 30 50 70 100
## 44.20906 69.59581 84.22733 92.24218 104.08624
##
## Upper 95 Percent Confidence Limits
## EXAM1
## 6 30 50 70 100
## 67.19343 76.78798 91.30754 112.44377 144.32634
alleffdf1 <- allEffects(model_multiple)
plot(alleffdf1)Predict nilai residual (ehat)
model_partial <- lm(EXAM1 ~ EXAM2 + EXAM3, df1)
summary(model_partial)##
## Call:
## lm(formula = EXAM1 ~ EXAM2 + EXAM3, data = df1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -11.5194 -0.5356 -0.0621 0.2932 11.6185
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 1.49704 3.35699 0.446 0.658
## EXAM2 0.02246 0.01616 1.390 0.171
## EXAM3 0.92941 0.02681 34.661 <2e-16 ***
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 3.753 on 51 degrees of freedom
## Multiple R-squared: 0.9903, Adjusted R-squared: 0.9899
## F-statistic: 2611 on 2 and 51 DF, p-value: < 2.2e-16
# predict residuals ehat
ehat = resid(model_partial)
# Y = gamma0 + beta1*ehat + v
lm(FINAL ~ ehat, df1) %>% summary##
## Call:
## lm(formula = FINAL ~ ehat, data = df1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -77.64 -71.06 -62.45 75.32 115.92
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 80.5444 10.7300 7.507 7.72e-10 ***
## ehat 0.7288 2.9421 0.248 0.805
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 78.85 on 52 degrees of freedom
## Multiple R-squared: 0.001179, Adjusted R-squared: -0.01803
## F-statistic: 0.06136 on 1 and 52 DF, p-value: 0.8053
Goodnes of fit (R-squared and adjusted R-squared)
summary(model_multiple)$r.squared## [1] 0.9969097
summary(model_multiple)$adj.r.squared## [1] 0.9967243
Confidence Interval
confint(model_multiple, level=0.95)## 2.5 % 97.5 %
## (Intercept) -4.78641462 11.31738637
## EXAM1 0.39356587 1.06398829
## EXAM2 -0.03145279 0.04736474
## EXAM3 0.91715616 1.55333939
kode berikutnya menggambarkan cara mengakses berbagai item keluaran regresi untuk persamaan.
library(kableExtra)
kable(data.frame(vcov(model_multiple)), align='c', digits=3,
caption="The coefficient covariance matrix",
col.names=c("(Intercept)", "EXAM1", "EXAM2","EXAM3"))| (Intercept) | EXAM1 | EXAM2 | EXAM3 | |
|---|---|---|---|---|
| (Intercept) | 16.070 | -0.042 | -0.074 | -0.080 |
| EXAM1 | -0.042 | 0.028 | -0.001 | -0.026 |
| EXAM2 | -0.074 | -0.001 | 0.000 | 0.001 |
| EXAM3 | -0.080 | -0.026 | 0.001 | 0.025 |
Hypotesis Testing
Proses pengujian hipotesis tentang parameter tunggal mirip dengan yang telah kita lihat dalam regresi sederhana, satu-satunya perbedaan yang terdiri dari jumlah derajat kebebasan. Sebagai contoh, mari kita uji signifikansi EXAM1 dari persamaan hasil tes psycologhy. Hipotesis diberikan dalam Persamaan
alpha <- 0.05
df <- model_multiple$df.residual
tcr <- qt(1-alpha/2, df)
b2 <- coef(model_multiple)[["EXAM1"]]
seb2 <- sqrt(vcov(model_multiple)[2,2])
t <- b2/seb2
print(tcr)## [1] 2.008559
print(t)## [1] 4.366775
T = 4.3668 dan tcr lebih kecil dari T maka hipotesis nol diterima. dan EXAM1 dari nilai EXAM1 tidak mempengaruhi nilai EXAM
Kita lihat dari EXAM 2 apakah ada pengaruh terhadap nilai akhir
alpha <- 0.05
df <- model_multiple$df.residual
tcr <- qt(1-alpha/2, df)
b2 <- coef(model_multiple)[["EXAM2"]]
seb2 <- sqrt(vcov(model_multiple)[3,3])
t <- b2/seb2
print(tcr)## [1] 2.008559
print(t)## [1] 0.4054946
diketahui bahwa nilai t lebih kecil dari nilai t maka EXAM2 mempengaruhi nilai akhir
Penilaian Akurasi Model
summary(model_multiple)$r.squared## [1] 0.9969097
4
Sehubungan dengan soal No 3, buatlah model regresi linear sederhana yang terbaik dari semua
kemungkinan variable (coba terapkan semua kemungkinan model, contohnya, kuardatik, log-log, dll sampai anda menemukan model terbaiknya)
Quadratic models
qEXAM2<-df1$EXAM2^2
qEXAM3<-df1$EXAM3^2
quadmod <- lm(FINAL~EXAM2+EXAM3+qEXAM2+qEXAM3 , data=df1)
summary(quadmod)##
## Call:
## lm(formula = FINAL ~ EXAM2 + EXAM3 + qEXAM2 + qEXAM3, data = df1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -7.2786 -2.6466 -0.3492 2.3734 14.4305
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -4.547e+01 1.271e+01 -3.577 0.000794 ***
## EXAM2 5.896e-01 1.325e-01 4.450 4.95e-05 ***
## EXAM3 2.571e+00 2.134e-01 12.048 2.92e-16 ***
## qEXAM2 -1.533e-03 3.494e-04 -4.388 6.08e-05 ***
## qEXAM3 -5.742e-03 1.945e-03 -2.952 0.004838 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.492 on 49 degrees of freedom
## Multiple R-squared: 0.9969, Adjusted R-squared: 0.9967
## F-statistic: 3998 on 4 and 49 DF, p-value: < 2.2e-16
Polynomial Models
qEXAM2<-df1$EXAM2^2
qEXAM3<-df1$EXAM3^2
polymod <- lm(FINAL~EXAM2+EXAM3+I(qEXAM2)+I(qEXAM3) , data=df1)
summary(quadmod)##
## Call:
## lm(formula = FINAL ~ EXAM2 + EXAM3 + qEXAM2 + qEXAM3, data = df1)
##
## Residuals:
## Min 1Q Median 3Q Max
## -7.2786 -2.6466 -0.3492 2.3734 14.4305
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) -4.547e+01 1.271e+01 -3.577 0.000794 ***
## EXAM2 5.896e-01 1.325e-01 4.450 4.95e-05 ***
## EXAM3 2.571e+00 2.134e-01 12.048 2.92e-16 ***
## qEXAM2 -1.533e-03 3.494e-04 -4.388 6.08e-05 ***
## qEXAM3 -5.742e-03 1.945e-03 -2.952 0.004838 **
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.492 on 49 degrees of freedom
## Multiple R-squared: 0.9969, Adjusted R-squared: 0.9967
## F-statistic: 3998 on 4 and 49 DF, p-value: < 2.2e-16
Linear-Log Models
library(xtable)
linlogmod<- lm(FINAL~log(EXAM1), data=df1)
tbl <- data.frame(xtable(linlogmod))
datatable(tbl,
caption = htmltools::tags$caption(
style = 'caption-side: bottom; text-align: center;',
htmltools::em('Table linear Log')),
options = list(dom='t'))EXAM1 = df1$EXAM1
b1 <- coef(linlogmod)[[1]]
b2 <- coef(linlogmod)[[2]]
pred_linlogmod<-predict(linlogmod, newdata = data.frame(EXAM1), interval="confidence")
plot(df1$EXAM1,df1$FINAL,xlab="EXAM1",ylab="FINAl")
lines(pred_linlogmod[,1]~df1$EXAM1,lty=1,col="black")
lines(pred_linlogmod[,2]~df1$EXAM1,lty=2,col="red")
lines(pred_linlogmod[,3]~df1$EXAM1,lty=2,col="red")Log-Linear Models
library(xtable)
library(kableExtra)
loglinmod1 <- lm(log(FINAL)~EXAM2, data=df1)
sumloglin<-summary(loglinmod1)
tbl <- data.frame(xtable(sumloglin))
datatable(tbl,
caption = htmltools::tags$caption(
style = 'caption-side: bottom; text-align: center;',
htmltools::em('Table Log Linear')),
options = list(dom='t'))Log-Log Models
loglogmod<-lm(log(FINAL)~log(EXAM3),df1)
b1 <- coef(loglogmod)[[1]]
b2 <- coef(loglogmod)[[2]]
sloglog<-summary(loglogmod)
tbl <- data.frame(xtable(sloglog))
datatable(tbl,
caption = htmltools::tags$caption(
style = 'caption-side: bottom; text-align: center;',
htmltools::em('Table Log Log poultry regression equation')),
options = list(dom='t'))R-squared
rgsq <- cor(df1$EXAM3,yhate)^2
rgsq## [1] 0.9939935