Created
May 5, 2020 13:20
-
-
Save rhilfi/0979e9b3795ec4a928de76e6b9163376 to your computer and use it in GitHub Desktop.
generateDataFrame_random_error_bias_a_bit_of_both
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##################################################################################################### | |
### This exercise should illustrate perfect agreement, random error and systematic error ### | |
##################################################################################################### | |
library(rio) | |
library(tidyverse) | |
library(ggplot2) | |
library(BlandAltmanLeh) | |
library(psych) | |
rnorm2 <- function(n,mean,sd) { mean+sd*scale(rnorm(n)) } # this function is just to make it possible to fix the values or rnorm https://stackoverflow.com/questions/18919091/generate-random-numbers-with-fixed-mean-and-sd?lq=1 | |
set.seed(1) # this is just in order that we get the same results, however, there might still be some differences because of difference MAC / PC | |
data<-tibble(ID=1:100, Ms_Perfect=rnorm(100, 80, 10), | |
Mr_Perfect=Ms_Perfect,Ms_Random=rnorm(100,80,10),Mr_Random=rnorm(100,80,10), | |
Ms_Bias=rnorm(100, 80,10),Mr_Bias=Ms_Bias-20, Ms_A.Bitofrandom<-rnorm(100,80,10) , | |
Mr_A.Bitofrandom<-Ms_A.Bitofrandom-(rnorm2(100,0,10)),Ms_A.Bitofrandomandbias<-rnorm(100,80,10) , | |
Mr_A.Bitofrandomandbias<-Ms_A.Bitofrandomandbias-(rnorm2(100,10,10)) ) | |
## Above we used the tidyverse approach, if you prefer the base R approach, you find this here. This is just for those confused by the command above. | |
ID =1:100 | |
#set.seed(1) # if we do not set the seed or would set an other seed, the rnorm commands would get different results everytime we run it. | |
Ms_Perfect<-rnorm(100, 80, 10) | |
Mr_Perfect<-Ms_Perfect | |
Ms_Random<-rnorm(100,80,10) | |
Mr_Random<-rnorm(100,80,10) | |
Ms_Bias<-rnorm(100, 80,10) | |
Mr_Bias<-Ms_Bias+20 | |
Ms_A.Bitofrandom<-rnorm(100,80,10) | |
Mr_A.Bitofrandom<-Ms_A.Bitofrandom-(rnorm2(100,0,10)) | |
Ms_A.Bitofrandomandbias<-rnorm(100,80,10) | |
Mr_A.Bitofrandomandbias<-Ms_A.Bitofrandomandbias-(rnorm2(100,10,10)) | |
# First, we create some data | |
data<-data.frame(ID, Ms_Perfect, Mr_Perfect,Ms_Random,Mr_Random,Ms_Bias,Mr_Bias ,Ms_A.Bitofrandom, Mr_A.Bitofrandom, Ms_A.Bitofrandomandbias, Mr_A.Bitofrandomandbias) | |
psych::describe(data_perfect_random_bias) | |
psych::describe(data) | |
################################################# | |
### Scatterplot for Ms and Mr Perfect ### | |
################################################# | |
library(ggplot2) | |
family_perfect<-ggplot(data=data, aes(x=Ms_Perfect, y=Mr_Perfect))+ | |
geom_point() + | |
coord_cartesian(xlim=c(60,110), ylim=c(60,110)) | |
print(family_perfect) | |
##################################################### | |
### Bland Altman Plot for Ms and Mr Perfect ### | |
##################################################### | |
bland.altman.plot(data$Ms_Perfect, data$Mr_Perfect, main="This is a Bland Altman Plot", xlab="Means Ms Perfect & Ms Perfect", ylab="Differences Ms Perfect - Mr Perfect") | |
####################################### | |
### ICC for Ms and Mr Perfect ### | |
####################################### | |
out<-psych::ICC(as.matrix(data[2:3])) | |
out | |
out$stats | |
out$MSW | |
################################################ | |
### Scatterplot for Ms and Mr Random ### | |
################################################ | |
family_random<-ggplot(data=data, aes(x=Ms_Random, y=Mr_Random))+ | |
geom_point()+ | |
coord_cartesian(xlim=c(60,110), ylim=c(60,110)) | |
print(family_random) | |
#################################################### | |
### Bland Altman Plot for Ms and Mr Random ### | |
#################################################### | |
bland.altman.plot(data$Ms_Random, data$Mr_Random, main="This is a Bland Altman Plot", xlab="Means Ms Random & Mr Random", ylab="Differences Ms Random - Mr Random") | |
######################################### | |
### ICC for Ms and Mr Random ### | |
######################################### | |
names(data) | |
out<-psych::ICC(as.matrix(data[4:5])) | |
out | |
out$stats | |
out$MSW | |
############################################### | |
### Scatterplot for Ms and Mr Bias ### | |
############################################### | |
family_bias<-ggplot(data=data, aes(x=Ms_Bias, y=Mr_Bias))+ | |
geom_point(aes(size=1.2), shape=1)+ xlim(40,100)+ylim(40,100)+ # https://ggplot2.tidyverse.org/reference/geom_point.html | |
stat_smooth(method="lm",fullrange=TRUE) + # https://stats.stackexchange.com/questions/36207/how-to-predict-or-extend-regression-lines-in-ggplot2 | |
theme(legend.position="none") | |
print(family_bias) | |
##################################################### | |
### Bland Altman Plot for Ms and Mr Bias ### | |
##################################################### | |
bland.altman.plot(data$Ms_Bias, data$Mr_Bias, main="This is a Bland Altman Plot", xlab="Means Ms Bias & Mr Peter", ylab="Differences Ms Bias & Mr Peter") | |
######################################### | |
### ICC for Ms and Mr Bias ### | |
######################################### | |
names(data) | |
out_bias<-psych::ICC(as.matrix(data[6:7]), lme=FALSE) | |
out_bias | |
out_bias$stats | |
out_bias$MSW | |
######################################################### | |
### Scatterplot for Ms and Mr A. Bitofrandom ### | |
######################################################### | |
family_a.bitofrandom<-ggplot(data=data, aes(x=Ms_A.Bitofrandom, y=Mr_A.Bitofrandom))+ | |
geom_point()+ | |
coord_cartesian(xlim=c(40,110), ylim=c(40,110))+ | |
geom_smooth(method=lm) | |
print(family_a.bitofrandom) | |
############################################################### | |
### Bland Altman Plot for Ms and Mr A. Bitofrandom ### | |
############################################################### | |
bland.altman.plot(data$Ms_A.Bitofrandom, data$Mr_A.Bitofrandom, main="This is a Bland Altman Plot", xlab="Means Ms A.Bitofrandom & Mr A.Bitofrandom", ylab="Differences Ms A.Bitofrandom & Mr A.Bitofrandom") | |
################################################# | |
### ICC for Ms and Mr A. BitofRandom ### | |
################################################# | |
names(data) | |
out<-psych::ICC(as.matrix(data[4:5])) | |
out | |
out$stats | |
out$MSW | |
############################################################### | |
### Scatterplot for Ms and Mr A. Bitofrandomandbias ### | |
############################################################### | |
family_a.bitofrandomandbias<-ggplot(data=data, aes(x=Ms_A.Bitofrandomandbias, y=Mr_A.Bitofrandomandbias))+ | |
geom_point()+ | |
coord_cartesian(xlim=c(40,110), ylim=c(40,110))+ | |
geom_smooth(method=lm) | |
print(family_a.bitofrandomandbias) | |
###################################################################### | |
### Bland Altman Plot for Ms and Mr A. Bitofrandomandbias ### | |
###################################################################### | |
bland.altman.plot(data$Ms_A.Bitofrandomandbias, data$Mr_A.Bitofrandomandbias, main="This is a Bland Altman Plot", xlab="Means Ms A.Bitofrandomandbias & Mr A.Bitofrandomandbias", ylab="Differences Ms A.Bitofrandomandbias & Mr A.Bitofrandomandbias") | |
######################################################## | |
### ICC for Ms and Mr A. BitofRandomandbias ### | |
######################################################## | |
names(data) | |
out<-psych::ICC(as.matrix(data[4:5])) | |
out | |
out$stats | |
out$MSW |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment