I think your confusion comes from the fact that the PCA truncation is going to reconstruct the full dimensions of the original matrix. If you want to only consider the first two columns of the data, then this has to be what you decompose with svd
.
The first example is a truncation of the iris
data using all 4 columns (as in your example) and then truncating with one PC:
dat <- as.matrix(iris[,-5])
s <- svd(dat)
plot(cumsum(s$d^2/sum(s$d^2))) # % explained variance
pc.use <- 1
recon <- s$u[,pc.use] %*% diag(s$d[pc.use], length(pc.use), length(pc.use)) %*% t(s$v[,pc.use])
x11(6,6)
par(mfcol=c(1,2), mar=c(1,1,1,1), oma=c(0,3,1,0))
zlim=range(dat, recon)
image(dat, zlim=zlim, yaxt="n", xaxt="n", ylab="", xlab="", main="Iris data")
axis(2, at=seq(0,1,,ncol(dat)), labels=colnames(dat))
image(recon, zlim=zlim, yaxt="n", xaxt="n", ylab="", xlab="", main="Truncated")
The second example is an svd
on only the first two columns of iris
, thus the reconstruction is also only going to have two columns. The reconstruction again uses the single leading PC:
dat <- as.matrix(iris[,-c(3:5)])
s <- svd(dat)
plot(cumsum(s$d^2/sum(s$d^2))) # % explained variance
pc.use <- 1
recon <- s$u[,pc.use] %*% diag(s$d[pc.use], length(pc.use), length(pc.use)) %*% t(s$v[,pc.use])
x11(6,6)
par(mfcol=c(1,2), mar=c(1,1,1,1), oma=c(0,3,1,0))
zlim=range(dat, recon)
image(dat, zlim=zlim, yaxt="n", xaxt="n", ylab="", xlab="", main="Iris data")
axis(2, at=seq(0,1,,ncol(dat)), labels=colnames(dat))
image(recon, zlim=zlim, yaxt="n", xaxt="n", ylab="", xlab="", main="Truncated")

?prcomp
. $\endgroup$svd
is the main algorithm behind PCA and thus the other post is asking a very similar question. By "dimension reduction", what is really meant is that a smaller number of linear predictors can be used to explain a large portion of the data. $\endgroup$