library(boot) library(bootstrap) library(MASS) # MASS berisi tools untuk fitting robust regression library(tcltk) library(stats) library(rpanel) ######################################################################################### ambil<-function() { data <- read.csv("F:/data.csv") plot<-plot(data[,2],data[,3], xlab="Euro",ylab="Rupiah",main="Plot Euro, dan Rupiah") } data <- read.csv("F:/data.csv") x <- data[,2] y <- data[,3] ######################################################################################### function seluruhlinier(){ plot(x, y) fred <- lm(y ~ x) curve(predict(fred, data.frame(x = x)), add = TRUE) beta1.hat <- coefficients(fred)[1] beta2.hat <- coefficients(fred)[2] n <- length(x) beta1.star <- double(nboot) beta2.star <- double(nboot) for (i in 1:nboot) { k.star <- sample(n, replace = TRUE) x.star <- x[k.star] y.star <- y[k.star] sally <- lm(y.star ~ x.star ) curve(predict(sally, data.frame(x.star = x)), add = TRUE, col = "plum") beta1.star[i] <- coefficients(sally)[1] beta2.star[i] <- coefficients(sally)[2] } points(x, y, pch = 16) curve(predict(fred, data.frame(x = x)), add = TRUE, lwd = 2) data.frame (beta1.hat,sd(beta1.star),beta2.hat,sd(beta2.star),beta2.hat / sd(beta2.star)) } ######################################################################################### function seluruhrobust(){ plot(x, y) fred <- rlm(y ~ x) curve(predict(fred, data.frame(x = x)), add = TRUE) beta1.hat <- coefficients(fred)[1] beta2.hat <- coefficients(fred)[2] n <- length(x) beta1.star <- double(nboot) beta2.star <- double(nboot) for (i in 1:nboot) { k.star <- sample(n, replace = TRUE) x.star <- x[k.star] y.star <- y[k.star] sally <- rlm(y.star ~ x.star ) curve(predict(sally, data.frame(x.star = x)), add = TRUE, col = "plum") beta1.star[i] <- coefficients(sally)[1] beta2.star[i] <- coefficients(sally)[2] } points(x, y, pch = 16) curve(predict(fred, data.frame(x = x)), add = TRUE, lwd = 2) data.frame (beta1.hat,sd(beta1.star),beta2.hat,sd(beta2.star),beta2.hat / sd(beta2.star)) } ######################################################################################### function linier2(){ nboot <- 2 seluruhlinier() } ######################################################################################### function linier30(){ nboot <- 30 seluruhlinier() } ########################################################################################### function linier100(){ nboot <- 100 seluruhlinier() } ############################################################################################ function robust2(){ nboot <- 2 seluruhrobust() } ######################################################################################### function linier30(){ nboot <- 30 seluruhrobust() } ########################################################################################### function linier100(){ nboot <- 100 seluruhrobust() } ########################################################################################### hitungan<-function() { #data.entry(y_hat_linier,y_hat_robust,Residual_linier,Residual_robust) } ############################################################################ kesimpulan<-function() { m4 <- tktoplevel() tkwm.geometry(m4, "+0+0") tkpack(fr <- tkframe(m4), side = "top") tkwm.title(m4, "Kesimpulan") tkpack(tklabel(fr, text = "Semakin besar bootstrap, semakin besar nilai beta1*, dan beta2*, Beta1 topi dan Beta topi2 akan tetap sama jika diulang terus menerus", width = "120"), side = "left") tkpack(fr <- tkframe(m4), side = "top") tkpack(tkbutton(m4, text = "OK", command = function() tkdestroy(m4)), side = "right") } linier<-function () { m2 <- tktoplevel() tkwm.geometry(m2, "+0+0") tkpack(fr <- tkframe(m2), side = "top") tkwm.title(m2, "Regresi Linier") tkpack(tklabel(fr, text = "OUTPUT", width = "50"), side = "left") tkpack(tkbutton(m2, text = "Regresi Linier B = 2",command=linier2), side = "left") tkpack(tkbutton(m2, text = "Regresi Linier B = 30",command=linier30), side = "left") tkpack(tkbutton(m2, text = "Regresi Linier B = 100",command=linier100), side = "left") tkpack(fr <- tkframe(m2), side = "top") tkpack(tkbutton(m2, text = "Kembali", command = function() tkdestroy(m2)), side = "right") } robust<-function() { m3 <- tktoplevel() tkwm.geometry(m3, "+0+0") tkpack(fr <- tkframe(m3), side = "top") tkwm.title(m3, "Regresi Robust") tkpack(tklabel(fr, text = "OUTPUT", width = "50"), side = "left") tkpack(tkbutton(m3, text = "Regresi robust B = 2",command=robust2), side = "left") tkpack(tkbutton(m3, text = "Regresi robust B = 30",command=robust30), side = "left") tkpack(tkbutton(m3, text = "Regresi robust B = 100",command=robust100), side = "left") tkpack(fr <- tkframe(m3), side = "top") tkpack(tkbutton(m3, text = "Kembali", command = function() tkdestroy(m3)), side = "right") } rp.messagebox("Selamat Datang di Aplikasi Regresi Bootstrap \nOleh: Edwin Wibisono (0800760383)", title="Bootstrap Regresi") local({ tt <- tktoplevel() tkwm.title(tt, "Bootstrap Regresi") frm <- tkframe(tt,borderwidth=400) f.but <- tkbutton(tt,text="Buka File",command=function()(local({fn<-choose.files(filters=Filters[c('R','txt','All'),],index=4) file.show(fn,header=fn,title='')})) ) tkpack(frm,f.but) }) #local({fn<-choose.files(filters=Filters[c('R','txt','All'),],index=4) #+ file.show(fn,header=fn,title='')}) m <- tktoplevel() tkwm.title(m, "Bootstrap Regresi") tkwm.geometry(m, "+0+0") tkpack(fr <- tkframe(m), side = "top") tkpack(tklabel(fr, text = "Aplikasi Bootstrap untuk Regresi Linier dan Regresi Robust", width = "50"), side = "left") tkpack(tkbutton(m, text = "Gambar Plot",command = ambil), side = "left") tkpack(tkbutton(m, text = "Regresi Linier",command =linier), side = "left") tkpack(tkbutton(m, text = "Regresi Robust",command =robust), side = "left") tkpack(tkbutton(m, text = "Kesimpulan",command =kesimpulan), side = "left") tkpack(fr <- tkframe(m), side = "top") tkpack(tkbutton(m, text = "Keluar", command = function() tkdestroy(m)), side = "right") ###########################