Demo entry 6761889

Calculate estimate and SE

   

Submitted by anonymous on Oct 06, 2018 at 20:55
Language: RConsole. Code size: 862 Bytes.

library(readxl)
setwd("C:/Users/Jiahanchen/Desktop/Course/BIOS650/BIOS650_hw2")
strokedata <- read_excel("C:/Users/Jiahanchen/Desktop/Course/BIOS650/BIOS650_hw2/strokedata.xlsx")

#conduct MLR
MLR <-lm (Depression ~ Fatalism + Age + Sex + R_E,strokedata)
summary(MLR)

#calculate the standard error

#initialize matrix
beta_hat<-matrix(c(6.54281,0.25471,-0.08839,0.51520,0.29460),nrow=5,ncol=1)
x<-strokedata[,c("Fatalism","Age","Sex","R_E")]
inter<-matrix(1,nrow=nrow(x),ncol=1)
x<-cbind(inter,x)
y<-strokedata[,c("Depression")]
x0<-matrix(c(1,mean(strokedata$Fatalism),70,1,1),nrow=1,ncol=5)
x<-as.matrix(x)
y<-as.matrix(y)

#calculate
y_hat<-x%*%beta_hat
e_hat<-y-y_hat
sigma_square_hat<-t(e_hat)%*%e_hat/(612-6)
var_beta<-as.numeric(sigma_square_hat)*solve(t(x)%*%x)
var<-x1%*%var_beta%*%t(x1)
se<-sqrt(var)
estimate<-x0%*%beta_hat

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).