[R-sig-Geo] R2 values from SSErr fit.variogram attribute

Sébastien Durand v8extra at gmail.com
Mon Mar 14 01:14:01 CET 2011


Hello to all, 

My problem is that I am trying to use R as a remplacement for a software not wanted any more.

What I am trying to do is 
1- Compute variogram model
2- Compare the result I get with R and the ones I am having with the other software.
3- See if I get better fit with R then with the other software.

1) So the first step is easy. I can provide you with my data if you wish but I dont know how to send it to the list.
2) In order for me to be able to compare both model (R and Software), I need the following information to be computed from R

•      Residual Sums of Squares (I beleive the software is using ordinary least square, not sure and that is my problem)
•      R2 value 

To find a solution I need your help!
I have been reading the Gstat manual, the vignette("gstat"), the r-geo-list... but I am still confused and 
I wish to know if I am in the rigth path.

So from my understanding, when "fit.variogram" is used, an SSErr value is computed an attributed to the output and that value will change based on the fit.method that is used
The default is 7 for the fit.method (weigthed least squares).

I guess the SSErr means Sum of Squared Errors (residuals I guess)?

I have found one answer on the list to help me compute the R2 from a fit.variogram that has used the fit.method=7...

Here it is for fit.method 7: 

r2findWLS <-function(fit, vario){
	# fit = fit.variogram output
	# vario = variogram output

	SSErr<-attr(fit,"SSErr")
	weig<-vario$np/vario$dist^2  
	# default method uses weights $N_h/h^2$ 
	# with $N_h$ the number of point pairs and $h$ the distance.
	SStot<- sum(weig*(vario$gamma-mean(vario$gamma))^2)
	R2<-1-SSErr/SStot
	return(R2)
}

So from that how do I get the results for R2 and the RSS usings both ordinary least square, the generalized least square and the weigthed least square...

Frankly my head hurts... 

I dont know if I am on the right track but, if I want to find the best fit using ordinary least square I have to use fit.method=6

Therefore : 

r2findOLS <-function(fit, vario){
	# fit = fit.variogram output
	# vario = variogram output

	SSErr<-attr(fit,"SSErr")
	# this method uses no weights
	# with $N_h$ the number of point pairs and $h$ the distance.
	SStot<- sum((vario$gamma-mean(vario$gamma))^2)
	print(SStot)
	R2<-1-SSErr/SStot
	return(R2)
}


Now my problem is that I dont seem to have correct results (and I am very far for it) when I compare both R results and the software results.

So what I am looking for is : 

1- Is my logic correct
2- What about the function
3- Could you provide me with code that you allow me to compute both these values.


A big thanks to all who wish to spend time and help me out!



More information about the R-sig-Geo mailing list