Kerri Colman - 8 months ago 41

R Question

I have not yet mastered the skill of writing functions and so am asking for help!

I would like to create a function that calculates the technical error of measurement (TEM) and the relative TEM across two columns.

I have seen previous posts about this, however they don't seem to do what i need them to do...

The first one: Technical error of measurement (TEM) for 3 or more participants is for 3 observers. If you try and apply this with just two observers if gives a different result.

The second one: Technical error of measurement in between two columns is what i am looking for but i would like it to be a function and not a manual code.

Can someone please help me write this into a function for my data:

`n <- 23`

D <- sum(MHDo1Ana$MHD.o1m1 - MHDo1Ana$MHD.o1m2)

D2 <- D^2

TEM <- sqrt((D2)/2*n)

TEM <- sqrt(((sum(MHDo1Ana$MHD.o1m1 - MHDo1Ana$MHD.o1m2)^2)) / 2 * n)

as well as a function for relative TEM?

My data is:

`MHD.o1m1 MHD.o1m2`

1 46.58 46.77

2 50.66 50.50

3 51.15 50.98

4 46.54 46.16

5 45.78 45.82

6 47.83 47.98

7 43.59 43.66

8 40.27 40.59

9 50.08 50.11

10 47.66 47.69

11 46.11 46.11

12 42.90 43.34

13 38.93 39.14

14 43.89 44.05

15 48.14 48.39

16 46.20 46.11

17 51.34 51.12

18 43.48 43.41

19 46.29 46.14

20 42.26 42.28

21 45.33 45.28

22 47.43 47.23

23 37.37 37.32

Thank you so much!

Answer Source

This should do the job:

```
TEM <- function(df, col1, col2){
sqrt(((sum(df[, col1] - df[, col2])^2)) / 2 * nrow(df))
}
```

First declare it by pasting it in your console, then use it with (adapted to your dataset):

```
TEM(MHDo1Ana, "MHD.o1m1", "MHD.o1m2")
```

Reproducible example below:

```
MHDo1Ana <- read.table(text=" MHD.o1m1 MHD.o1m2
1 46.58 46.77
2 50.66 50.50
3 51.15 50.98
4 46.54 46.16
5 45.78 45.82
6 47.83 47.98
7 43.59 43.66
8 40.27 40.59
9 50.08 50.11
10 47.66 47.69
11 46.11 46.11
12 42.90 43.34
13 38.93 39.14
14 43.89 44.05
15 48.14 48.39
16 46.20 46.11
17 51.34 51.12
18 43.48 43.41
19 46.29 46.14
20 42.26 42.28
21 45.33 45.28
22 47.43 47.23
23 37.37 37.32")
TEM(MHDo1Ana, "MHD.o1m1", "MHD.o1m2")
[1] 1.254731
```

Does this solve your problem?