# Untitled

unknown
plain_text
2 years ago
5.1 kB
1
Indexable
Never
```#TRATAMENTO DE DADOS

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

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

#box.plot continuas
par(mfrow = c(3, ncol(dados_cont)/3), mar=c(2,2,2,2), cex=0.5)

#variavel resposta

#correlações
library(corrplot)
dev.off()
corrplot(a, method = 'color', addCoef.col = 'black',
number.cex = 0.45, tl.cex = 0.5, tl.col="black")

#eliminar Systolic e Diastolic Pressure

#ANALISE PRELIMINAR DO MODELO COMPLETO
#plot(mrl.comp)
summary(mrl.comp)
extractAIC(mrl.comp)

library(gvlma)
gvlma(mrl.comp)

#boxcox para por y normal
library(MASS)
b <- boxcox(lm(y ~ 1))
lambda <- b\$x[which.max(b\$y)]
#lambda=0.30303...
y2 <- (y^lambda-1)/lambda

#teste de ajustamento do y
shapiro.test(y) #p-value = 1.101e-05
shapiro.test(y2) #p-value = 0.7035 > 0.25

#modelo com y normal
#plot(mrl.comp2) #mais proximo de normal

#eliminar outliers - nao tem pelo box plot
boxplot(y2)

##dados de treino e de teste
set.seed(73)
teste_ind<-sort(sample(nrows,0.2*nrows))

#Treino
y_treino <- y2[-teste_ind]

#Teste
y_teste <- y2[teste_ind]

#matrizes x
n=length(y_treino)

summary(mrl.full)
extractAIC(mrl.full)

#stepforward
#primeira int
mrl.base <-lm(y_treino~1,

mrl.stepforward <- step(mrl.base,
scope = list(upper = formula(mrl.full),
lower = formula(mrl.base)),
direction = "forward", trace="FALSE")

summary(mrl.stepforward)
extractAIC(mrl.stepforward)
formula(mrl.stepforward)

#segunda int
mrl.int2 <- lm(y_treino ~ (Shock.Type + Sex + Diastolic.Pressure + Systolic.Pressure +
Mean.Arterial.Pressure + Age + Body.Surface.Area)^2,

mrl.stepforward2 <- step(mrl.stepforward,
scope = list(upper = formula(mrl.int2),
lower = formula(mrl.stepforward)),
direction = "forward", trace="FALSE")
summary(mrl.stepforward2)
extractAIC(mrl.stepforward2)
formula(mrl.stepforward2)

#terceira int
mrl.int3 <- update(mrl.stepforward2, .~. + Sex:Body.Surface.Area:Diastolic.Pressure)

mrl.stepforward3 <- step(mrl.stepforward2,
scope = list(upper = formula(mrl.int3),
lower = formula(mrl.stepforward2)),
direction = "forward", trace="FALSE")
summary(mrl.stepforward3)
extractAIC(mrl.stepforward3)
formula(mrl.stepforward3)

predictions2 <- predict(mrl.stepforward3, newdata = dados_teste)
d2<-data.frame(predictions2, y=y_teste)
ggplot(d2, aes(predictions2, y)) +
geom_point(shape = 16, size = 3, show.legend = FALSE)

ri <- y_teste - predictions2
sum(ri^2)/length(y_teste)

#################

#stepbackward
mrl.stepbackward <- step(mrl.full, direction = "backward", trace=FALSE)

summary(mrl.stepbackward)
extractAIC(mrl.stepbackward)
formula(mrl.stepbackward)

library(ggplot2)
predictions <- predict(mrl.full, newdata = dados_treino)
d<-data.frame(predictions, y=y_treino)
ggplot(d, aes(predictions, y)) +
geom_point(shape = 16, size = 3, show.legend = FALSE)

library(emmans)

emmip(mrl.stepbackward,Sex ~ Age, cov.reduce=range) #não há interação
emmip(mrl.stepbackward,Shock.Type ~ Age, cov.reduce=range) #não há interação
emmip(mrl.full,Sex ~ Body.Surface.Area, cov.reduce=range) #não há interação
emmip(mrl.full,Age ~ Height, cov.reduce=range) #não há interação

```