Last data update: 2014.03.03

R: Calculates the relative weight of each observation
calcWeightsR Documentation

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

A matrix with the updated weights column

Author(s)

Tal Carmi, Liat Gaziel

Examples

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)
  }

Results


R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(ACCLMA)
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/ACCLMA/calcWeights.Rd_%03d_medium.png", width=480, height=480)
> ### Name: calcWeights
> ### Title: Calculates the relative weight of each observation
> ### Aliases: calcWeights
> ### Keywords: ~kwd1 ~kwd2
> 
> ### ** Examples
> 
> 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)
  X   Y Weights
1 1 5.5    0.50
2 3 7.0    0.25
3 4 8.0    0.25
> 
> ## 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)
+   }
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)
}
> 
> 
> 
> 
> 
> dev.off()
null device 
          1 
>