lars
in Stata matches the defaults in R (standardize all variables to have unit L2 norm and include an intercept). The intercept itself, however, is not reported in either package. I think I figured out how to get it in R using predict
and setting all the covariates to zero, but I can't seem to successfully replicate this with Stata.
Here's the Stata code (which calls R using rsource
from SSC) showing the equivalence:
set more off
capture ssc install rsource
sysuse auto, clear
saveold "/Users/dmasterov/Desktop/auto_v12.dta", replace
rsource, terminator(END_OF_R)
library(lars)
library(foreign)
data<-read.dta("/Users/dmasterov/Desktop/auto_v12.dta")
attach(data)
x<-data[c("mpg","headroom","trunk","weight","length","turn","displacement")]
x<-as.matrix(x)
y<-price
lars <- lars(x,y, type="lar", normalize = TRUE, intercept = TRUE)
lars
coefficients(lars)
summary(lars)
predict(lars,data.frame(mpg=0,headroom=0,trunk=0,weight=0,length=0,turn=0,displacement=0),s=7)$fit
detach(data)
END_OF_R
sysuse auto, clear
lars price mpg headroom trunk weight length turn displacement, algorithm(lars)
At first, my intuition was that you should be able to take the lars
coefficients and use them in constrained regression in Stata to get the intercept. However, I cannot seem to match the number I get in R with this method. I get essentially the intercept you would get if you included no constant, but a vector of ones in X whose coefficients you penalized.