Skip to content

Instantly share code, notes, and snippets.

@gaolei786
Created June 10, 2013 12:32
Show Gist options
  • Save gaolei786/5748381 to your computer and use it in GitHub Desktop.
Save gaolei786/5748381 to your computer and use it in GitHub Desktop.
正交变换与数据保密
set.seed(123)
x <- seq(1, 20, length = 20)
y <- 4 + 3 * seq(1, 20, length = 20) + rnorm(20, 0, 6)
plot(x, y)
k <- 2
tstMat <- array(runif(k), dim=c(k,k))
A1 <- qr.Q(qr(tstMat))
A2 <- 2
x1 <- cbind(1, x) %*% A1
y1 <- y * A2
k <- 20
tstMat <- array(runif(k), dim=c(k,k))
B <- qr.Q(qr(tstMat))
y2 <- B %*% y1
x2 <- B %*% x1
x3 <- x2 %*% t(A1)
y3 <- y2 * 1/A2
lm1 <- lm(y ~ x)
lm2 <- lm(y3 ~ x3-1)
par(mfrow = c(1, 2))
plot(x, y, xlim = c(-40, 30), ylim = c(-40, 70), pch = 6, xlab = "", ylab = '')
abline(a= lm1$coefficients[1], b =lm1$coefficients[2] )
plot(x3[, 2],y3, xlim = c(-40, 30), ylim = c(-40, 70), pch = 11,col = 2, xlab = '', ylab = '')
abline(a= lm2$coefficients[1], b =lm2$coefficients[2], col = 2 )
plot(lm1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment