Home‎ > ‎Schedule‎ > ‎

R commands for Feb 23

# Hinton's study time data p.265

studytimedata = data.frame(study.time=c(40,43,18,10,25,33,27,17,30,47),exam.score=c(58,73,56,47,58,54,45,32,68,69))

plot(studytimedata[,1:2])

#Calculating pearson's r via z-scores

popsd <- function(x,n)(sqrt(var(x)*(n-1)/n))

n = length(studytimedata$exam.score)

studytimedata$study.time.z    =  (studytimedata$study.time - mean(studytimedata$study.time))/popsd(studytimedata$study.time,n)

studytimedata$exam.score.z  = (studytimedata$exam.score - mean(studytimedata$exam.score))/popsd(studytimedata$exam.score,n)

plot(studytimedata[,3:4])

r = sum(studytimedata$study.time.z * studytimedata$exam.score.z)/n

df = n -2

#Or use the cor command

cor(studytimedata$study.time,studytimedata$exam.score)

#Calculating practical significance

SP = sum((studytimedata$study.time - mean(studytimedata$study.time))*(studytimedata$exam.score - mean(studytimedata$exam.score)))

SSx = sum((studytimedata$study.time - mean(studytimedata$study.time))^2)

SSy = sum((studytimedata$exam.score - mean(studytimedata$exam.score))^2)

r2 = SP^2/(SSx*SSy)

# relation to r

r = sqrt(r2)
r = SP/sqrt(SSx*SSy)

# regression model Y = A + B*X

B = r*(sd(studytimedata$exam.score)/sd(studytimedata$study.time))

lm(studytimedata$exam.score  ~ studytimedata$study.time)



Comments