averageSameXs: Unifies all same X observations

Description Usage Arguments Value Author(s) Examples

View source: R/lmafunctions.r

Description

The function unifies all the observations with the same X values into one obseravtion with regard to the weights of each observation

Usage

1

Arguments

mat

A matrix containing the X,Y,Weight columns after the calcWeights function been used on it

Value

Data set with only row for each X value

Author(s)

Tal Carmi, Liat Gaziel

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
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")
averageSameXs(mydata)

## The function is currently defined as
function (mat) 
{
    n <- size(mat)
    temp <- matrix(NA, nrow = n, ncol = 3)
    line <- 1
    i <- 1
    while (i <= n) {
        j <- i
        sumY <- 0
        sumWeights <- 0
        while (j <= n & mat[i, 1] == mat[j, 1]) {
            sumY <- sumY + mat[j, 2] * mat[j, 3]
            sumWeights <- sumWeights + mat[j, 3]
            j <- j + 1
        }
        temp[line, 1] <- mat[i, 1]
        temp[line, 2] <- sumY/sumWeights
        temp[line, 3] <- sumWeights
        line <- line + 1
        i <- j
    }
    temp <- as.data.frame(temp[1:(line - 1), ])
    names(temp)[1] = "X"
    names(temp)[2] = "Y"
    names(temp)[3] = "Weights"
    return(temp)
  }

ACCLMA documentation built on May 29, 2017, 10:52 p.m.