[R] what is this averaging function called ?, has R a built in function for it ?

madr madrazel at interia.pl
Tue Dec 7 12:50:09 CET 2010


my input is from csv file:

fname= 'test'
csvdata = read.table(file=paste(fname,'.csv',sep=''),head=FALSE)
x = csvdata$V1
y = csvdata$V2

I know that this group is not about php , but I managed to make function
from above a lot faster, and I'm still cannot operate R at a sufficient
level to recreate in in this language. Fit function in R that kinda
resembles it is smooth.spline, but that function seems to go in a strange
directions when data density becomes low

But I think I would get desired behaviour in smooth.spline if for x axis I
put matrix of simple 1,2,3 sequence

here is revised php code:

function smooth($in,$smooth=500) {
	if (count(current($in))!=2) exit('wrong array');
	timer();

	foreach($in as &$v) {
		$v[0] = (string)$v[0];
		if (!isset($y[$v[0]])) {
			$y[$v[0]] = 0;
			$z[$v[0]] = 0;
		}
		$y[$v[0]] += $v[1];
		++$z[$v[0]];
	}
	unset($in);
	ksort($y, SORT_NUMERIC);
	ksort($z, SORT_NUMERIC);
	$x = array_keys($z);
	$y = array_values($y);
	$z = array_values($z);
	$count = count($z);
	echo n.$count.' : ';
	for ($k=0;$k<$count;++$k) {
		if (!($k % 1000)) echo $k.' ';
		$u = $d = 0;
		$usum = $dsum = 0;
		$walk = 0;
		while (true) {
			++$walk;
			if (isset($z[$k-$walk]) and $z[$k]+$z[$k-$walk]+$usum<$smooth) {
				$usum += $z[$k-$walk];
				$u += $y[$k-$walk];
			}
			else break;
		}
		$walk = 0;
		while (true) {
			++$walk;
			if (isset($z[$k+$walk]) and $z[$k]+$z[$k+$walk]+$dsum<$smooth) {
				$dsum += $z[$k+$walk];
				$d += $y[$k+$walk];
			}
			else break;
		}
		$out[$k] = ($y[$k]+$u+$d)/($z[$k]+$usum+$dsum);
	}
	echo ' : '.timer().n;
	return array($x,$out);
}
-- 
View this message in context: http://r.789695.n4.nabble.com/what-is-this-averaging-function-called-has-R-a-built-in-function-for-it-tp3072826p3076215.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list