Regresi Piecewise Diskontinu: R-code


Berikut adalah code di R untuk pembahasan regresi piecewise diskontinue di sini. ################### #Simulasi piecewise regression ################### #Generate data
x.1<-runif(50,0,70)
x.2<-runif(20,70,90)
y.1<-150-1.5*x.1+rnorm(50,3,6)
y.2<-400-5*x.2+rnorm(20,2,5)
x<-c(x.1,x.2)
y<-c(y.1,y.2)
#Plot data
plot(x,y)   #continuous piecewise
y.2<-y.2-100 # geser y.2 ke bawah sejauh 100, untuk diskontinuitas
y.discontinue<-c(y.1,y.2)
plot(x,y.discontinue)   #discontinuous piecewise

######################################## #Data Soal 9.4 Mendenhall & Sincich, 7th ed, hal.475 ########################################
hours<-c(1,5,10,20,30,40,45,50,60,70,80,90,100,110,120,130,140)
cells<-c(2,3,4,5,4,6,8,9,10,18,16,18,20,14,12,13,9)
data.growth<-data.frame(hours,cells)
data.growth
plot(hours,cells)

y<-cells
x1<-hours
x2<-NA
#Membuat dummy variabel x2 untuk Model 1 ##################################
x2[x1> 60] <- 1
x2[x1<= 60] <- 0

x2star<-(x1-60)*x2
#Fit regresi piecewise untuk Model 1 ############################
model.1<-lm(y~x1+x2star+x2)
summary(model.1)
#Bandingkan dengan Model 2 ####################### #Membuat dummy variabel x2 untuk Model 2 ###################################
x2[x1> 100] <- 1
x2[x1<= 100] <- 0

x2star<-(x1-100)*x2
#Fit regresi piecewise untuk Model 2 ############################
model.2<-lm(y~x1+x2star+x2)
summary(model.2)
 
########END########

No comments:

Post a Comment

Relevant & Respectful Comments Only.