multigrp_dist_struc: Construct the distance structure for the multiple groups.

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/multigrp_dist_struc.R

Description

This function can be used to calculate the distance structure for multiple groups. The output of this function can be feed into the argument distmat of the main functions kwaymatching and tripletmatching.

Usage

1
multigrp_dist_struc(.data, grouplabel, components, wgts)

Arguments

.data

The data frame or matrix of the dataset.

grouplabel

The information on the group structure of the units. See description of kwaymatching for details on the argument.

components

A list specifying the components of the distance structure. Each element of the list is a character vector of column names of the .data on which 'distance' will be calculated. The element names specify the function to be used to calculate to distance of two groups. Element named 'prop' indicates the propensity distance where the propensity is calculated from the specified variable. Element named 'mahal' or 'Mahalanobis' for rank based Mahalanobis distance.

User can spacify their own distance function. For example, a function myDist should be a function of two arguments: a logical vector of the first group indicator and a data matrix. It should return a numeric matrix of size number of units of first group\timesnumber of units of second group. See details for an example.

wgts

A non-negative numeric vector of weights of the components.

Details

This function can be used to get distance structure suitable for creating the distances between the units of the groups.

For an example of the kind of user defined distance function that can be used see smahal below.

Value

A list describing the distance structure. For detail see the description of the argument distmat in the function kwaymatching.

Author(s)

Bikram Karmakar

See Also

kwaymatching, tripletmatching

Examples

 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
	data(Dodgeram)
	
	# Example distance structure
	components <- list(prop = c("AGE", "SEX.2", "FR.pass", "REST_USE1", "ROLLOVER1",
	            "IMPACT3", "SP_LIMIT", "DR_DRINK", "FIRE_EXP1.1"), 
	            mahal = c("SEX.2", "AGE", "SP_LIMIT", "DR_DRINK"), 				
	            mahal = c("IMPACT3", "REST_USE1"))
	wgts <- c(5, 8, 20)
	
	distmat <- multigrp_dist_struc(Dodgeram, 
            grouplabel = c("NOSAB","optSAB","WITHSABS"), components, wgts)
	
	## Propensity score caliper can be implemented mannually
	
	distmat <- multigrp_dist_struc(Dodgeram, 
            grouplabel = c("NOSAB","optSAB","WITHSABS"), 
            list(mahal = c("SEX.2", "AGE", "SP_LIMIT", "DR_DRINK"), 				
                mahal = c("IMPACT3", "REST_USE1")), wgts=c(2, 5))
	distmat_prop <- multigrp_dist_struc(Dodgeram, 
                grouplabel = c("NOSAB", "optSAB", "WITHSABS"), 
              list(prop = c("AGE", "SEX.2", "FR.pass", "REST_USE1", "ROLLOVER1",
	            "IMPACT3", "SP_LIMIT", "DR_DRINK", "FIRE_EXP1.1")), 1)
		
	## Distance structure with caliper
	for(i in 1:length(distmat))
			distmat[[i]][distmat_prop[[i]]>.2] <- 100*max(distmat[[i]])
			
				
									   
## An example function for argument detail.

smahal <-  function(z,X){
        X<-as.matrix(X)
		n<-dim(X)[1]
		rownames(X)<-1:n
		k<-dim(X)[2]
		m<-sum(z)
		for (j in 1:k) X[,j]<-rank(X[,j])
		cv<-cov(X)
		vuntied<-var(1:n)
		rat<-sqrt(vuntied/diag(cv))
		cv<-diag(rat)
		out<-matrix(NA,m,n-m)
		Xc<-X[z==0,,drop=FALSE]
		Xt<-X[z==1,,drop=FALSE]
		rownames(out)<-rownames(X)[z==1]
		colnames(out)<-rownames(X)[z==0]
		#library(MASS)
		icov<-ginv(cv)
		for (i in 1:m) 
			out[i,]<-mahalanobis(Xc,Xt[i,],icov,inverted=T)
		sqrt(out)
	}

approxmatch documentation built on March 31, 2020, 5:17 p.m.