Skip to content

Instantly share code, notes, and snippets.

@abikoushi
Created August 29, 2024 08:20
Show Gist options
  • Save abikoushi/b0d5b3c873485026447503e5629f1911 to your computer and use it in GitHub Desktop.
Save abikoushi/b0d5b3c873485026447503e5629f1911 to your computer and use it in GitHub Desktop.
linear model with "change point"
x <- seq(-1,1, length.out=100)
x2 <- 1*(x>0)
X <- cbind(x, x2, x*x2)
B <- c(0.5,2,-1)
mu <- c(X%*%B)
set.seed(2024);y <- mu + rnorm(100,0,0.1)
fit <- lm.fit(X,y)
muhat <- c(X%*%c(fit$coefficients))
png("cplm.png")
plot(x,y)
lines(x,mu, lty=2, col="royalblue")
lines(x,muhat)
legend("topleft", c("estimates","true"),lty=2:1,lwd=1.5,
col=c("royalblue","black"))
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment