set.seed(123)n<-100beta<-1.5alpha<-1# Scores along the principal-axis directiont<-rnorm(n, mean =0, sd =2)# Noise orthogonal to the principal axise<-rnorm(n, mean =0, sd =0.5)# Direction vector for the principal axis and the orthogonal directionv1<-c(1, beta)/sqrt(1+beta^2)v2<-c(-beta, 1)/sqrt(1+beta^2)x<-t*v1[1]+e*v2[1]y<-alpha+t*v1[2]+e*v2[2]plot(x, y, asp =1, pch =16)
# Slopes of the first and second principal axesmajor_axis_slope<-eigen_vectors[2, 1]/eigen_vectors[1, 1]minor_axis_slope<-eigen_vectors[2, 2]/eigen_vectors[1, 2]# Calculate intercepts so that the lines pass through the data centermajor_axis_intercept<-mean(y)-major_axis_slope*mean(x)minor_axis_intercept<-mean(y)-minor_axis_slope*mean(x)# Plot data points and the main directions of variationplot(x, y, asp =1, pch =16, main ="Principal Axis Regression")# Add the first principal axisabline( a =major_axis_intercept, b =major_axis_slope, col =2, lwd =3)# Add the second principal axisabline( a =minor_axis_intercept, b =minor_axis_slope, col =4, lwd =3)
# Mean-centered coordinatesz<-scale(cbind(x, y), center =TRUE, scale =FALSE)plot(x, y, asp =1, pch =16)abline(h =mean(y), v =mean(x), col ="#D55E00", lwd =2, lty =2)# Draw vectors from the mean point to each pointfor(iin1:n){segments(mean(x), mean(y), x[i], y[i], col ="gray70")}
# Mean-centered coordinatesz<-scale(cbind(x, y), center =TRUE, scale =FALSE)# Mean pointmx<-mean(x)my<-mean(y)u<-c(0.8, 0.6)# direction vector uu<-u/sqrt(sum(u^2))# normalize to a unit vector just in case# Projection scalar s_i for each point in direction us<-as.vector(z%*%u)# Projected point: mean point + s_i uproj_x<-mx+s*u[1]proj_y<-my+s*u[2]# Drawplot(x, y, asp =1, pch =16, type ="n")usr<-par("usr")# plotting range# Grid with spacing of 1abline( v =seq(floor(usr[1]), ceiling(usr[2]), by =1), col ="gray70", lty ="dotted")abline( h =seq(floor(usr[3]), ceiling(usr[4]), by =1), col ="gray70", lty ="dotted")# Vertical and horizontal lines through the meanabline(h =my, v =mx, col ="#D55E00", lwd =2, lty =2)# Line representing direction uif(abs(u[1])<.Machine$double.eps){abline( v =mx, col =adjustcolor("#0072B2", alpha.f =0.5), lwd =3, lty ="dashed")}else{abline( a =my-(u[2]/u[1])*mx, b =u[2]/u[1], col =adjustcolor("#0072B2", alpha.f =0.5), lwd =3, lty ="dashed")}# Mean-centered vector z_i for each pointfor(iinseq_along(x)){segments(mx, my, x[i], y[i], col ="gray90")}# Perpendicular from each point to the projection point:# residual not explained by direction ufor(iinseq_along(x)){segments(x[i],y[i],proj_x[i],proj_y[i], col =adjustcolor("#009E73", alpha.f =0.5), lwd =2)}# From mean point to projected point: s_i ufor(iinseq_along(x)){segments(mx,my,proj_x[i],proj_y[i], col =adjustcolor("#CC79A7", alpha.f =0.1), lwd =4)}# Original pointspoints(x, y, pch =16, col =adjustcolor("black", alpha.f =0.5))# Arrow for direction vector uarrows(mx,my,mx+u[1],my+u[2], col ="#0072B2", lwd =4, length =0.2)
set.seed(123)n<-100beta<-1.5alpha<-1t<-rnorm(n, mean =0, sd =2)e<-rnorm(n, mean =0, sd =0.5)v1<-c(1, beta)/sqrt(1+beta^2)v2<-c(-beta, 1)/sqrt(1+beta^2)x<-t*v1[1]+e*v2[1]y<-alpha+t*v1[2]+e*v2[2]plot(x, y, asp =1, pch =16)
# Covariance matrix and eigenvectorsS<-cov(cbind(x, y))eg<-eigen(S, symmetric =TRUE)# Direction of the first principal axisu1<-eg$vectors[, 1]if(u1[1]<0){u1<--u1}
# Data centercenter<-c(mean(x), mean(y))u0<-c(1, 0.4)# comparison direction vectoru0<-u0/sqrt(sum(u0^2))# normalize to a unit vector# Function to calculate projected points on the lineget_foot_points<-function(x, y, u, center){X<-cbind(x-center[1], y-center[2])score<-as.vector(X%*%u)# calculate projection scores for each pointfoot_centered<-outer(score, u)# calculate projected points from the scoresfoot<-sweep(foot_centered, 2, center, "+")# add the centerfoot}# Calculate projected points for each pointfoot0<-get_foot_points(x, y, u0, center)foot1<-get_foot_points(x, y, u1, center)
データの上に、比較用の直線と各点からその直線への射影点をプロットしてみます。
plot(x, y, asp =1, pch =16, xlab ="x", ylab ="y")abline( a =center[2]-(u0[2]/u0[1])*center[1], b =u0[2]/u0[1], col ="gray30", lwd =2)segments(x, y, foot0[, 1], foot0[, 2], col ="gray70")
次に、ここに第1主軸の線と、各点から第1主軸への射影点を追加してみます。
plot(x, y, asp =1, pch =16, xlab ="x", ylab ="y")# Line and distances for the comparison directionabline( a =center[2]-(u0[2]/u0[1])*center[1], b =u0[2]/u0[1], col ="gray30", lwd =2)segments(x, y, foot0[, 1], foot0[, 2], col ="gray70")# Line and distances for the first principal-axis directionabline( a =center[2]-(u1[2]/u1[1])*center[1], b =u1[2]/u1[1], col =2, lwd =3)segments(x, y, foot1[, 1], foot1[, 2], col =3)
このときの各点から直線までの距離の二乗和を計算して比較します。
# Sum of squared distances from the line for the comparison directionsum(rowSums((cbind(x, y)-foot0)^2))
[1] 117.576
# Sum of squared distances from the line for the first principal-axis directionsum(rowSums((cbind(x, y)-foot1)^2))
u2<-eigen_vectors[, 2]if(u2[1]<0){u2<--u2}plot(x, y, asp =1, pch =16, xlab ="x", ylab ="y")# First principal-axis lineabline( a =center[2]-(u1[2]/u1[1])*center[1], b =u1[2]/u1[1], col =2, lwd =3)# Second principal-axis lineabline( a =center[2]-(u2[2]/u2[1])*center[1], b =u2[2]/u2[1], col =4, lwd =3)
第1主軸はデータのばらつきが最も大きい方向を表し、第2主軸はそれに直交する方向を表します。
まとめ
Principal axis regressionは、2変量データの主要な変動方向を直線として捉える方法です。
Warton, David I., Ian J. Wright, Daniel S. Falster, と Mark Westoby. 2006年. 「Bivariate Line-Fitting Methods for Allometry」. Biological Reviews 81 (2): 259–91. https://doi.org/10.1017/S1464793106007007.