Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
2.9 kB
0
Indexable
Never
dados <- read.delim("Trabalho10_Dados.txt", header= FALSE, sep="", )
dados <- dados[,2:13]

names(dados)<- c("Age", "Height", "Sex", "Survival", "Shock.Type", "Systolic.Pressure", 
                 "Mean.Arterial.Pressure", "Heart.Rate", "Diastolic.Pressure", "Mean.Central.Venous", 
                 "Body.Surface.Index", "Cardiac.Index")


rows <- nrow(dados)
even_rows <- seq_len(rows) %% 2
dados_inicial <- dados[even_rows == 1,]
X <- cbind(1,data.matrix(dados_inicial))

qualitativas <- c("Sex", "Survival", "Shock.Type")

#for (i in names(dados_inicial)){
 # if(!(i %in% qualitativas)) {
  #  print(i)
   # outliers <- boxplot(names(i ~ Shock.Type, dados_inicial)$out
    #dados_inicial1 <- dados_inicial[!(dados_inicial$i %in% outliers), ]
  #}
#}

#no final?
par(mfrow = c(1, ncol(dados_inicial)))
lapply(1:ncol(dados_inicial), function(i) boxplot(dados_inicial[i])$out)

#for (i in 1:ncol(X)){
#X[,i]<- X[!X[,i] %in% boxplot.stats(X)$out]}



#sex
x3 <- X[,4]
x3[x3 == 2] <- 0
x3

#survival
x4 <- X[,5]
x4[x4 == 3] <- 0
x3

#shock
x5_2 <- X[,6]
x5_2[x5_2 != 2] <- 0
x5_2[x5_2 == 2] <- 1
x5_2
x5_3 <- X[,6]
x5_3[x5_3 != 3] <- 0
x5_3[x5_3 == 3] <- 1
x5_3
x5_4 <- X[,6]
x5_4[x5_4 != 4] <- 0
x5_4[x5_4 == 4] <- 1
x5_4
x5_5 <- X[,6]
x5_5[x5_5 != 5] <- 0
x5_5[x5_5 == 5] <- 1
x5_5
x5_6 <- X[,6]
x5_6[x5_6 != 6] <- 0
x5_6[x5_6 == 6] <- 1
x5_6

#avaliar sex, shock e survival antes de eliminar
X <- cbind(X[,1:3], x3, x4, x5_2, x5_3, x5_4, x5_5, x5_6, X[,7:ncol(X)-1])
y <- matrix(dados_inicial$Cardiac.Index)

n=length(y)
p=ncol(X)

betahat = solve(t(X) %*% X) %*% t(X) %*% y
betahat

###

SSE = t(y - X %*% betahat) %*% (y - X %*% betahat)
MSE = SSE/(n-p)

varbetahat = c(MSE) * solve(t(X) %*% X)

fittedval = X %*% betahat
simpleres = y - fittedval
w = cbind(fittedval, simpleres)
colnames(w) = c("fitted values","residuals")
print(w)
##

mrl <- lm(formula = y ~ Age + Height + Sex + Shock.Type + 
            Systolic.Pressure + Mean.Arterial.Pressure + Heart.Rate + 
            Diastolic.Pressure + Mean.Central.Venous + Body.Surface.Index + Cardiac.Index +
            Appearance.Time + Mean.Circulation.Time + Urinary.Output +
            Plasma.Volume.Index + Red.Cell.Index + Hemoglobin + Hematocrit, dados_inicial)
summary(mrl)

C <- cbind(0,diag(p-1))
m <- 0
SSE_h <- t(y)%*%(diag(n) - X%*%solve(t(X)%*%X)%*%t(X))%*%y
f <- c()
for(i in 2:p-1){ 
  ci <- t(matrix(C[i,])) #linha
  Q_i <- t(ci%*%betahat-m)%*%solve(ci%*%solve(t(X)%*%X)%*%t(ci))%*%(ci%*%betahat-m)
  f_i <- (Q_i/1)/(SSE_h/(n-p))
  f <- c(f,f_i)
}
1-pf(f,1,n-p)                                   

#?? correlação

library(corrplot)
a = round(cor(X[,2:ncol(X)]),  
digits=2)
corrplot(a, order = "hclust", method = 'color', addCoef.col = 'black', 
         number.cex = 0.45, tl.cex = 0.5, tl.col="black")