Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Now can obtain standard error for constant on Multiple Regression wit…

…h Covariance matrix
  • Loading branch information...
commit b65d14e5983242bdf103d03a4906965dc4430413 1 parent 11e69f2
@clbustos authored
View
19 doc_latex/manual/equations.tex
@@ -6,6 +6,23 @@ \section{Convention}
p &= \text{proportion inside a sample}\\
P &= \text{proportion inside a population}
\end{align*}
+\section{Ruby::Regression::Multiple}
+
+To compute the standard error of coefficients, you obtain the estimated variance-covariance matrix of error.
+
+Let \mathbf{X} be matrix of predictors data, including a constant column; \mathbf{MSE} as mean square error; SSE as Sum of squares of errors; n the number of cases; p as number of predictors
+
+\begin{equation}
+\mathbf{MSE}=\frac{SSE}{n-p-1}
+\end{equation}
+
+\begin{equation}
+\mathbf{E}=(\mathbf{X'}\mathbf{X})^-1\mathbf{MSE}
+\end{equation}
+
+The root squares of diagonal should be standard errors
+
+
\section{Ruby::SRS}
Finite Poblation correction is used on standard error calculation on poblation below 10.000. Function
\begin{verbatim}
@@ -28,7 +45,7 @@ \section{Ruby::SRS}
fpc_{sd} = \sqrt{\frac{N-n} {N-1}}
\label{fpc}
\end{equation}
-with n as sam and N as pop.
+with n as sample size and N as population size.
\subsection{Sample Size estimation for proportions}
View
2  lib/statsample/regression/multiple/baseengine.rb
@@ -125,7 +125,7 @@ def coeffs_se
out={}
mse=sse.quo(df_e)
coeffs.each {|k,v|
- out[k]=Math::sqrt(mse/(@ds[k].sum_of_squares*tolerance(k)))
+ out[k]=Math::sqrt(mse/(@ds[k].sum_of_squares * tolerance(k)))
}
out
end
View
70 lib/statsample/regression/multiple/matrixengine.rb
@@ -36,21 +36,37 @@ def initialize(matrix,y_var, opts=Hash.new)
matrix.extend Statsample::CovariateMatrix
raise "#{y_var} variable should be on data" unless matrix.fields.include? y_var
- @matrix_cor=matrix.correlation
+
+ if matrix.type==:covariance
+ @matrix_cov=matrix
+ @matrix_cor=matrix.correlation
+ @no_covariance=false
+ else
+ @matrix_cor=matrix
+ @matrix_cov=matrix
+ @no_covariance=true
+ end
@y_var=y_var
@fields=matrix.fields-[y_var]
+
@n_predictors=@fields.size
- @matrix=matrix
- @matrix_x= matrix.submatrix(@fields)
- @matrix_y = matrix.submatrix(@fields, [y_var])
- @matrix_y_cor=@matrix_cor.submatrix(@fields, [y_var])
- @result_matrix=@matrix_x.inverse * @matrix_y
- @y_sd=Math::sqrt(@matrix.submatrix([y_var])[0,0])
- @x_sd=@matrix_x.row_size.times.inject({}) {|ac,i|
- ac[@matrix_x.fields[i]]=Math::sqrt(@matrix_x[i,i])
+
+ @matrix_x= @matrix_cor.submatrix(@fields)
+ @matrix_x_cov= @matrix_cov.submatrix(@fields)
+
+ @matrix_y = @matrix_cor.submatrix(@fields, [y_var])
+ @matrix_y_cov = @matrix_cov.submatrix(@fields, [y_var])
+
+
+
+ @y_sd=Math::sqrt(@matrix_cov.submatrix([y_var])[0,0])
+
+ @x_sd=@n_predictors.times.inject({}) {|ac,i|
+ ac[@matrix_x_cov.fields[i]]=Math::sqrt(@matrix_x_cov[i,i])
ac;
}
+
@cases=nil
@x_mean=@fields.inject({}) {|ac,f|
ac[f]=0.0
@@ -64,13 +80,15 @@ def initialize(matrix,y_var, opts=Hash.new)
opts.each{|k,v|
self.send("#{k}=",v) if self.respond_to? k
}
+ result_matrix=@matrix_x_cov.inverse * @matrix_y_cov
+
if matrix.type==:covariance
- @coeffs=@result_matrix.column(0).to_a
+ @coeffs=result_matrix.column(0).to_a
@coeffs_stan=coeffs.collect {|k,v|
coeffs[k]*@x_sd[k].quo(@y_sd)
}
else
- @coeffs_stan=@result_matrix.column(0).to_a
+ @coeffs_stan=result_matrix.column(0).to_a
@coeffs=standarized_coeffs.collect {|k,v|
standarized_coeffs[k]*@y_sd.quo(@x_sd[k])
@@ -87,8 +105,7 @@ def cases
# * 1-(|R| / |R_x|) or
# * Sum(b_i*r_yi)
def r2
- @n_predictors.times.inject(0) {|ac,i| ac+@coeffs_stan[i]* @matrix_y_cor[i,0]}
- #1-(@matrix.correlation.determinant.quo(@matrix_x.correlation.determinant))
+ @n_predictors.times.inject(0) {|ac,i| ac+@coeffs_stan[i]* @matrix_y[i,0]}
end
def r
Math::sqrt(r2)
@@ -141,9 +158,32 @@ def coeffs_se
}
out
end
-# Standard error for constant
+ # Standard error for constant.
+ # Recreate the estimaded variance-covariance matrix
+ # using means, standard deviation and covariance matrix
def constant_se
- nil
+ nil if @no_covariance
+ means=@x_mean
+ #means[@y_var]=@y_mean
+ means[:constant]=1
+ sd=@x_sd
+ #sd[@y_var]=@y_sd
+ sd[:constant]=0
+ fields=[:constant]+@matrix_cov.fields-[@y_var]
+ xt_x=Matrix.rows(fields.collect {|i|
+ fields.collect {|j|
+ if i==:constant or j==:constant
+ cov=0
+ elsif i==j
+ cov=sd[i]**2
+ else
+ cov=@matrix_cov.submatrix(i..i,j..j)[0,0]
+ end
+ cov*(@cases-1)+@cases*means[i]*means[j]
+ }
+ })
+ matrix=xt_x.inverse * mse
+ matrix.collect {|i| Math::sqrt(i) if i>0 }[0,0]
end
def to_reportbuilder(generator)
View
2  test/test_regression.rb
@@ -163,7 +163,7 @@ def test_regression_matrix
covariance=Statsample::Bivariate.covariance_matrix(ds)
lr=Statsample::Regression::Multiple::MatrixEngine.new(covariance,'y', :y_mean=>@y.mean, :x_mean=>{'a'=>ds['a'].mean, 'b'=>ds['b'].mean, 'c'=>ds['c'].mean}, :cases=>@a.size)
- model_test_matrix(lr , "covariance matrix")
+ model_test(lr , "covariance matrix")
end
def test_regression_rubyengine
@a=[nil,1,3,2,4,3,5,4,6,5,7].to_vector(:scale)
Please sign in to comment.
Something went wrong with that request. Please try again.