File: varest.r

package info (click to toggle)
r-cran-sampling 2.11-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,340 kB
  • sloc: ansic: 21; makefile: 2
file content (29 lines) | stat: -rw-r--r-- 783 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
varest<-function(Ys,Xs=NULL,pik,w=NULL)
{
 if (any(is.na(pik))) 
        stop("there are missing values in pik")
 if (any(is.na(Ys))) 
        stop("there are missing values in y")
 if (length(Ys) != length(pik)) 
        stop("y and pik have different sizes")
if(!is.null(Xs))
 {if(is.data.frame(Xs)) Xs=as.matrix(Xs)
  if(is.vector(Xs) & (length(Ys)!= length(Xs)))  
        stop("x and y have different sizes")
  if(is.matrix(Xs) & (length(Ys) != nrow(Xs)))  
        stop("x and y have different sizes")
}
 a=(1-pik)/sum(1-pik)
 if(is.null(Xs))
	{A=sum(a*Ys/pik)
	 var=sum((1-pik)*(Ys/pik-A)^2)/(1-sum(a^2))
	}
 else 
   {B=t(Xs*w) 
   beta=ginv(B%*%Xs)%*%B%*%Ys
   e=Ys-Xs%*%beta
   A=sum(a*e/pik)
   var=sum((1-pik)*(e/pik-A)^2)/(1-sum(a^2))
}
var
}