
 datos <-scan("/home/profesor/Escritorio/AD1/cleveland_2",sep=" ")
## datos <-scan("/home/profesor/Descargas/cleveland_2",sep=" ")
 lista<- which(datos==99999); length(lista) ## posicion final de cada individuo
 table(c(lista,diff(lista))) ## comprobar si las muestras tienen el mismo nº de variables

 X<- matrix(datos, ncol=76, byrow=T) ## prepara los datos como una matriz
 ## X[1:5,]
 ## TRANSFORMAR EN DATA.FRAME
 X<- data.frame(X)

 ## SELECCIONAR VARIABLES
 lista<-  c(1,3 ,4, 9 , 10   ,12   ,16   ,19   ,32   ,38  ,40 ,41, 44 ,51 ,58)
 Y<- X[, lista]

 dim(Y)

 ## DAR NOMBRES A LAS VARIABLES
 variables<- c("ID", "age","sex","cp" ,"trestbps" ,"chol" ,"fbs","restecg" ,"thalach" , "exang" ,"oldpeak" , "slope","ca","thal","num")

 length(variables)
 names(Y)<- variables
 ## DESCRIPCION DEL FICHERO DE DATOS
 summary(Y)

 ## TRANSFORMAR VARIABLES CUALITATIVAS A FACTORES
 ## Variable num (variable objetivop)
 table(Y$num) ## describir la variable

 ## agrupar las categorías 1,2,3,4 en 1
 table(Y$num[which(Y$num>1)])
 Y$num[which(Y$num>1)]<- 1
 table(Y$num)

 ## pasar a factor y poner etiquetas
 Y$num<-factor(Y$num, labels=c("no", "si"))
 table(Y$num)
 
##Sexo: 4 sex: sex (1 = male; 0 = female)
 table(Y$sex)
 Y$sex<-factor(Y$num, labels=c("female", "male"))
 table(Y$sex)

 ## 9 cp: chest pain type 
 	## Value 1: typical angina ;  Value 2: atypical angina 
 	## Value 3: non-anginal pain; Value 4: asymptomatic 
 table(Y$cp)
 Y$cp<- factor(Y$cp, labels= c("angina_tipica", "a_atipica", "no_dolor", "no_sintomas"))
 table(Y$cp)

 ## 10 trestbps   mm Hg
 ## 12 chol: mg/l

 ## 16 fbs: (1 = true; 0 = false) 
 table(Y$fbs)
 Y$fbs<- factor(Y$fbs, labels= c("no", "si"))
 table(Y$fbs)

 ## 19 restecg: -- Value 0: Value 1: 0.05 mV; Value 2: 
  table(Y$restecg)
  Y$restecg<-factor(Y$restecg, labels= c("normal", "V1", "V2"))
  table(Y$restecg)
  Y$restecg[which(Y$restecg=="V1")]<- "V2"
  table(Y$restecg)
  table(Y$restecg)
  Y$restecg<- as.factor(as.vector(Y$restecg))
  table(Y$restecg)

## 38 exang: exercise induced angina (1 = yes; 0 = no) 
table(Y$exang)
 Y$exang<- factor(Y$exang, labels= c("no", "si"))
 table(Y$exang)

 ## 41 slope the slope of the peak exercise ST segment 
 # Value 1: upsloping ; Value 2: flat ; Value 3: downsloping
  table(Y$slope)
  Y$slope<-factor(Y$slope, labels= c("up", "flat", "down"))
  table(Y$slope)

 ## 44 ca: number of major vessels (0-3) colored by flourosopy 
  table(Y$ca)
  Y$ca[which(Y$ca==-9)]<- NA ## pasar -9 a perdido
  table(Y$ca)

 ## 51 thal: 3 = normal; 6 = fixed defect; 7 = reversable defect 
  table(Y$thal)
  Y$thal[which(Y$thal==-9)]<- NA ## pasar -9 a perdido
  table(Y$thal)
  Y$thal<-factor(Y$thal, labels= c("normal", "no_rever", "reversible"))
  table(Y$thal)

 CL<- Y
 save("CL", file="/home/profesor/Escritorio/AD/Cleveland/CL.RData")

 ## ANALISIS DE DATOS
 summary(Y) ## descripcion de los datos


## CUANTITATIVAS
 cuanti<- which(sapply(Y, is.numeric)== "TRUE"); cuanti ## cuantitativas
 cuanti<- cuanti[-1] ## se quita ID
 names(Y)[cuanti]

 cuali<- which(sapply(Y, is.factor)== "TRUE"); cuali ## cualitativas
 cuali<- cuali[-length(cuali)] ## se quita "num"
 names(Y)[cuali]


 ## Analisis de las Variables cuantitativas
 j<- 2
 for (j in cuanti) {
      print(names(Y)[j])
      print( round(numSummary(Y[,j], groups=Y$num)$table,2))
      t<- t.test(Y[,j] ~ Y$num, alternative='two.sided', conf.level=.95, var.equal=FALSE)
      w<- wilcox.test(Y[,names(Y)[j]] ~ Y$num, alternative="two.sided", data=Y)
      cat("p-valor t_Stu= ", round(t$p.value,4), "; p-valor Wilcoxon= ", round(w$p.value,4),"\n")
      cat("###################", "\n") }
load("/home/profesor/Escritorio/cleveland_2")
cuali
names(Y)
j<- 3
for (j in c(cuali,13)) {
      print(names(Y)[j])
     tabla<- table(Y[, names(Y)[j]], Y[,"num"])
     print(tabla)
     print(round(rowPercents(tabla),2))
test<- chisq.test(tabla)
      cat("p-valor Chi-cuadrado= ", round(test$p.value,4),"\n")
if (min(test$expected)<4) cat("minima frecuencia esperada= ", round(min(test$expected),1),"\n")
      cat("###################", "\n") }

