Section 21 Linear model

21.1 lm

  • We can conduct the t-test using the function lm

  • The function lm does linear modelling of continuous data

  • Fit a linear model using the function lm to compare if the mean difference for SBP between Group A and B is significantly different.

  • Examine the R object of the lm outputs.

  • Compare the lm outputs with the t.test outputs.

21.2 Syntax

\[ \large lm(SBP \sim Group, \space data=pBP) \]

21.3 R Code

BP <- read.csv('data/BP.csv')
str(BP)

index <- which(BP$Group=='A' | BP$Group=='B')
pBP <- BP[index,]
pBP$Group <- as.factor(as.character(pBP$Group))

with(data=pBP, tapply(X = SBP, INDEX = Group, FUN = mean, na.rm=TRUE))
with(data=pBP, tapply(X = SBP, INDEX = Group, FUN = sd, na.rm=TRUE))
plot(SBP ~ Group, data=pBP, col=c('orange','blue'))

lm_BP <- lm(SBP ~ Group, data = pBP)