I haven't seen code to do it yet but it is very simple to calculate with a 
LinearModel from GLM  Below is some code to calculate r-squared or adjusted 
r-squared from a linear model in GLM calculated from a dataframe or from a 
model calculated without a data frame.  At the bottom is some simple code 
to apply these functions.  It uses the Abs2Fun() from the NumericFuns 
package to calculate the sum of squares quickly.

using DataFrames
using GLM

import DataFrames.DataFrameRegressionModel
import GLM.LinearModel
import GLM.residuals
using NumericFuns


function 
rsquared(mod::DataFrameRegressionModel{LinearModel{DensePredQR{Float64}},Float64})
SStot=sum(Abs2Fun(),mod.model.rr.y)
SSres=sum(Abs2Fun(),residuals(mod))
return (1-(SSres/SStot))
end

function rsquared(mod::LinearModel{DensePredQR{Float64}})
SStot=sum(Abs2Fun(),mod.rr.y)
SSres=sum(Abs2Fun(),residuals(mod))
return (1-(SSres/SStot))
end

function 
adjrsquared(mod::DataFrameRegressionModel{LinearModel{DensePredQR{Float64}},Float64})
SStot=sum(Abs2Fun(),mod.model.rr.y)
SSres=sum(Abs2Fun(),residuals(mod))
n=size(mod.model.rr.y,1)  #number of samples
p=size(mod.mm.m,2)-1      #number of variables besides constant (assumes 
intercept)
return 1- ( (SSres/(n-p-1)) / (SStot/(n-1)) )
end

function adjrsquared(mod::LinearModel{DensePredQR{Float64}})
SStot=sum(Abs2Fun(),mod.rr.y)
SSres=sum(Abs2Fun(),residuals(mod))
n=size(mod.rr.y,1)    #number of samples
p=size(mod.pp.X,2)-1  #number of variables besides constant (assumes 
intercept)
return 1- ( (SSres/(n-p-1)) / (SStot/(n-1)) )
end


dd=randn(1000,7);
df=convert(DataFrame,dd);
aa=fit(LinearModel,x1~x2+x3+x4+x5+x6,df)
bb=fit(LinearModel,aa.mm.m,aa.model.rr.y)

rsquared(aa)
adjrsquared(aa)

rsquared(bb)
adjrsquared(bb)


On Wednesday, August 6, 2014 5:33:15 PM UTC-6, Jason Solack wrote:
>
> Hello everyone.  I am looking for a way to run this regression and 
> retrieve the corresponding r squared value.  Is there a package out there 
> that does this?
>
> Thank you for the help!
>
> Jason
>

Reply via email to