Learn R Programming

ACCLMA (version 1.0)

calcWeights: Calculates the relative weight of each observation

Description

This function receives a matrix after the averageSameXs function has been used on it and calculates the relative weight of each observation by dividing its weight by the total weights. If at least one observation doesn't have a weight specified the function assumes all the observations are of equal weight

Usage

calcWeights(mat)

Arguments

mat
a matrix after the averageSameXs function has been used on it

Value

Details

References

See Also

Examples

Run this code
d <- c(1,1,3,4)
e <- c(5,6,7,8)
f <- c(1,1,1,1)
mydata <- data.frame(d,e,f)
names(mydata) <- c("X","Y","Weight")
mydata<-averageSameXs(mydata)
calcWeights(mydata)

## The function is currently defined as
function (mat) 
{
    n <- size(mat)
    zeroWeight <- 0
    sumWeight <- 0
    for (i in 1:n) {
        if (is.null(mat[i, 3]) || is.na(mat[i, 3]) || mat[i, 
            3] == 0) {
            zeroWeight <- 1
        }
        sumWeight <- sumWeight + mat[i, 3]
    }
    if (zeroWeight == 1) {
        mat[3] = 1/n
        return(mat)
    }
    for (i in 1:n) {
        mat[i, 3] <- mat[i, 3]/sumWeight
    }
    return(mat)
  }

Run the code above in your browser using DataLab