Covariate assignment to k clusters using the coordinate descent algorithm. This function is a wrapper for the C function linreg_coord_clus

linrclus(Y, X, k, coefs, clus, clusmns, nC = 1, x = FALSE)

Arguments

Y

vector of outcome variable

X

matrix of covariates. Should not include 1's for the intercept

k

number of clusters

coefs

vector of coefficients as starting values. Should not include the intercept.

clus

vector of covariate cluster assignments as starting values

clusmns

vector k cluster parameter centers

nC

first nC-1 covariates in X not to cluster. Must be at least 1 for the intercept

x

a logical for returning the design matrix

Value

clus cluster assignments

coefs vector of coefficients as starting values

clusmns vector of cluster means

Examples

set.seed(14) #Generate data N = 1000; (bets = rep(-2:2,4)); p = length(bets); X = matrix(rnorm(N*p),N,p)
#> [1] -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2 -2 -1 0 1 2
Y = cbind(1,X)%*%matrix(c(0.5,bets),ncol = 1) begin_v<- rep(NA,p) for (j in 1:p) { begin_v[j] = stats::coef(lm(Y~X[,j]))[2] } set.seed(12); klus_obj<- kmeans(begin_v,centers = 5) linrclus(Y,X,k=5,coefs=c(0,begin_v),clus=klus_obj$cluster,clusmns=klus_obj$centers)
#> Inner iter = 1, RSS = 660.88739, and reldev = 0.30341 #> Inner iter = 2, RSS = 660.88739, and reldev = 0.00000
#> $clus #> [1] 2 1 3 5 4 2 1 3 5 4 2 1 3 5 4 2 1 3 5 4 #> #> $coefs #> [1] 0.5365218 -1.9155504 -1.0994377 -0.1097203 1.2567410 1.8776724 #> [7] -1.7917068 -1.0014693 -0.2690255 1.0173286 1.8275640 -1.8684473 #> [13] -0.8663424 0.1365382 0.5607767 1.9694359 -1.7534427 -0.8249476 #> [19] 0.2198973 0.8241155 1.7659304 #> #> $clusmns #> [1] -0.948049272 -1.832286807 -0.005577581 1.860150685 0.914740445 #>