R/caiman2-internal.R

Defines functions .caiman.pred .caiman.train .comb .dcaiman.pred .dcaiman.train .genBanco .genBancoC .genCentroide .genEspace .genhiddenSpace .genmin .genXtX .getlabel

library(tidyverse)
.caiman.pred <-
function(CT, newx) {
  classes = CT[['classes']]
  Xtx = CT[['xtx']]
  m = CT[['centroid']]
  K = genhiddenSpace(classes, newx, Xtx, m)
  genmin(CT$classes, K)
}
.caiman.train <-
function(y, x) {
  classes = levels(y)
  df = x %>% as.data.frame()
  df$y = y 
  
  list(xtx=genXtX(df, classes), centroid=genCentroide(df, classes), classes=classes)
}
.comb <-
function(df, classes, a) {
  y = df$y
  K = .genEspace(df, classes) %>% as.data.frame()
  df = K
  df$y = y
  KK = .genEspace(df, classes)
  
  combK = a*K + (1-a)*KK
  
  return(combK)
}
.dcaiman.pred <-
function(CT, x, a = NULL) {
  classes = CT[['classes']]
  xtx = CT[['xtx']]
  xtk = CT[['xtk']]
  mx = CT[['cx']]
  mk = CT[['ck']]
  if(is.null(a)) {
    a = CT[['a']]
  }
  
  valx = genhiddenSpace(classes, x = x, xxt = xtx, m = mx)
  valy = genhiddenSpace(classes, x = valx, xxt=xtk, m = mk)
  
  k = genhiddenSpace(classes, x, xtx, m = mx)
  
  kk = genhiddenSpace(classes, k, xtk, m = mk)
  kf = a*k + (1-a)*kk
  genmin(classes, kf)
  
}
.dcaiman.train <-
function(y, xtrain, xvalidation, grid) {
  classes = levels(y)
  df = xtrain %>% as.data.frame()
  df$y = y
  a = grid
  
  
  xtx = genXtX(df, classes)
  centroidx = genCentroide(df, classes)
  
  k = genEspace(df, classes) %>% as.data.frame()
  k$y = y
  xtxk = genXtX(k, classes)
  centroidk = genCentroide(k, classes)
  
  valx = genhiddenSpace(levels(y), x = xvalidation, xxt = xtx, m = centroidx)
  valy = genhiddenSpace(levels(y), x = valx, xxt=xtk, m = centroidk)
  
  erro = numeric(length(a))
  
  
  for(i in seq_along(a)) {
    valfinal = a[i]*valx + (1-a[i])*valy
    erro[i] = mean(validacao$V58!=genmin(levels(y), valfinal))
    
  }
  
  
  afinal = a[which.min(erro)]
  
  return(list(xtx=xtx,xtk=xtk,cx=centroidx, ck=centroidk, a=afinal, classes=classes))
  
}
.genBanco <-
function(df, classes) {
  bancos = list()
  for(i in classes) {
    bancos[[i]] = df %>% filter(y==i) %>% select(-y) %>% as.matrix()
  }
  return(bancos)
}
.genBancoC <-
function(df, classes) {
  bancos = list()
  for(i in classes) {
    bancos[[i]] = df %>% filter(y==i) %>% select(-y) %>% scale(center=T, scale=F) %>% 
      as.matrix()
  }
  return(bancos)
}
.genCentroide <-
function(df, classes) {
  centroide = list()
  for(i in classes) {
    centroide[[i]] = df %>% filter(y==i) %>% select(-y) %>% apply(FUN=mean, MARGIN=2) %>%
      as.matrix()
  }
  return(centroide)
}
.genEspace <-
function(df, classes) {
  K = matrix(0, nrow=nrow(df), ncol=length(classes))
  colnames(K) = classes
  
  m = .genCentroide(df, classes)
  xtx = .genXtX(df, classes)
  xm = df %>% select(-y) %>% as.matrix()
  y = df$y
  
  for(i in 1:nrow(df)) {
    for(g in classes) {
      h = t(cbind(xm[i,])-m[[g]])%*%xtx[[g]]%*%(cbind(xm[i,])-m[[g]])
      if(y[i]==g) {
        K[i,g] = h/(1-h)
      }else{
        K[i,g] = h
      }
    }
  }
  return(K)
}
.genhiddenSpace <-
function(classes, x, xxt, m) {
  K = matrix(0, nrow=nrow(x), ncol=length(classes))
  colnames(K) = classes
  xm = x %>% as.matrix()
  
  
  for(i in 1:nrow(x)) {
    for(g in classes) {
      h = t(cbind(xm[i,])-m[[g]])%*%xxt[[g]]%*%(cbind(xm[i,])-m[[g]])
      K[i,g] = h
      
      
    }
  }
  return(K)
}
.genmin <-
function(classes, Space) {
  K = Space
  pred = apply(K, FUN=which.min, MARGIN=1) 
  pred = .getlabel(classes, pred)
  return(pred)
}
.genXtX <-
function(df, classes) {
  banco = .genBancoC(df, classes)
  XtX = list()
  for(i in classes) {
    XtX[[i]] = solve(t(banco[[i]]%>%as.matrix())%*%(banco[[i]]%>%as.matrix()))
  }
  return(XtX)
}
.getlabel <-
function(classe, pred) {
    sapply(pred, FUN=function(classe, x){classe[x]}, classe=classe)
  }
.Random.seed <-
c(403L, 10L, -106294311L, 533437419L, -462849446L, 506956828L, 
-1718691553L, -1203648139L, -1065433076L, 1192268258L, 1057483453L, 
802202935L, -679507490L, -303034600L, -643217797L, -631569383L, 
544191656L, 639077974L, -1291474223L, 399002243L, 1518567698L, 
2097685668L, -744557465L, -1150779971L, 858193716L, -1897587558L, 
-1320787227L, 1939414143L, -1975249402L, -1304518256L, 1235377203L, 
883094481L, -1493354112L, 1326945374L, 297517993L, 1933253211L, 
-166887830L, 621298444L, -123460273L, 1042112005L, -1184045892L, 
-1545333358L, -2139009395L, 335103175L, 2062033326L, -1837790200L, 
1459273739L, 439284969L, 1807225784L, 1608309862L, -1121010431L, 
-773366893L, -673612734L, 1916879860L, 724618743L, -121859603L, 
-2016796028L, 1136432298L, 943063573L, 2147122863L, -945537866L, 
-511866144L, 25179683L, 1556232513L, -1005771728L, -2106278322L, 
182069945L, -609644853L, -20220038L, -956694916L, -1323664705L, 
565255061L, 1722178476L, 145543554L, 1789073245L, 767064663L, 
-2000490178L, 230166904L, -1349981605L, -948986439L, 1756872712L, 
284164790L, 58366449L, -1637068765L, 1044181106L, -1019358524L, 
-509066105L, 1216936029L, 1107336788L, 2091900026L, -1997994043L, 
226396447L, 656328870L, -1565660624L, 1178775763L, -1166754063L, 
1973116448L, -648025922L, 2032889033L, -765835653L, 929449034L, 
2095287468L, 1374804911L, -1196253339L, 64304412L, 508206450L, 
1214305773L, 665909799L, 1937873486L, -360587608L, -1855916885L, 
1975186889L, -1637697064L, 484028550L, 1161674081L, 1344196851L, 
-285237406L, -833119788L, -30446377L, 1554001613L, -618264988L, 
1373307978L, 484151733L, 615947535L, 654121494L, -1001425728L, 
1303696003L, -766083423L, -1482821488L, 650976110L, 2090386713L, 
1777694123L, 1269175194L, -523891876L, 1230424671L, 1949020725L, 
254695884L, 214806818L, 1642017661L, -1878503305L, -2119821922L, 
-1694733224L, 184703419L, 784605017L, -228746008L, 466896150L, 
-1374234991L, 947821379L, -1910913454L, -1983125276L, 2069723943L, 
-1342582915L, -537222540L, -1402717094L, 692241573L, -1071997249L, 
-1187904186L, -2134012976L, -1931409933L, 1931899153L, 1193139520L, 
-217244386L, -1721267735L, -309843173L, 275100970L, 1662611020L, 
-350438897L, -76128955L, 247693052L, -625781422L, 881662669L, 
-204377465L, -1806084754L, 342724424L, -501089461L, 623633065L, 
-94091272L, 113911590L, 1168201793L, 741521875L, -810118270L, 
-594192460L, -925970633L, -1707686355L, -932989116L, -1797634966L, 
1038589653L, -158181265L, 1103927670L, -1380687200L, 1026494307L, 
-302808063L, 1876897648L, 33580814L, 323109241L, 793072907L, 
-72364486L, -1277369028L, 549366399L, 1524183509L, -375730196L, 
-1097109182L, 537636509L, -1672221929L, 974903934L, -1956115912L, 
1588907291L, 787855993L, 1944090312L, -728264970L, -81574095L, 
1511235427L, -490506958L, -2109847932L, 1460753095L, 1923510685L, 
-1470647020L, 22109114L, 1438199301L, 1776373727L, -473438618L, 
-331410960L, -2032058605L, -1809252175L, 1729545056L, 668651774L, 
-115384183L, 68169659L, -884762230L, 265556412L, 1678806066L, 
467720000L, 1603982004L, 1154301256L, -530065734L, 1015382800L, 
1157470444L, -782455724L, 1747176210L, 651898272L, -1903544372L, 
1446490976L, -1476502334L, -1566490072L, 867122604L, 269011772L, 
1152573298L, -1422688464L, 2074051620L, 293880376L, -450083110L, 
-987724912L, -1252878084L, 5955988L, 1642242882L, 1179097600L, 
-1697717188L, -784897456L, 1696203938L, 852784968L, 1931525644L, 
-1491697956L, 1706229842L, 1633517696L, 602708820L, 490284008L, 
713220858L, 2112233296L, -1447083988L, 498446708L, 1351586450L, 
-964664736L, -1669757812L, -1419250336L, 856967394L, -1995407192L, 
453077708L, 2052974076L, -842891342L, 1583699952L, 1437844932L, 
-1509058216L, -1467042438L, 911959472L, 1332832700L, -757237164L, 
416042434L, 1155283744L, -1113158724L, -481999792L, 93724642L, 
704226152L, -165320884L, -1253868804L, -1334134030L, 566456576L, 
-2004364172L, 1159780872L, 1843784122L, -584074800L, 231222252L, 
-559315052L, 1809001682L, 1007962208L, 1198694732L, 1777723168L, 
-1360975230L, -865154584L, 812192748L, -339976644L, -1960863118L, 
-26976400L, -174550748L, 302599736L, 1246322074L, -347343408L, 
1333399996L, 2056217236L, 1935226498L, -197520960L, -234520772L, 
-455674224L, -777563358L, -1525692920L, -800639220L, -1948584036L, 
-619569134L, -1392991360L, 1996939028L, -144373464L, -81584070L, 
-1693411376L, 863988332L, -1182200780L, 812389522L, -399082144L, 
-1474039476L, 5941728L, 1641529250L, 903679912L, 844067980L, 
-1500744772L, 1072453746L, 1230107632L, -817086396L, 1104041048L, 
1369346106L, 1271316592L, 1646391868L, -1860011244L, 1173028546L, 
-1271922976L, -1408600964L, -709148464L, -2142096478L, 214471208L, 
1307911436L, -804873284L, 1803994418L, -711226176L, -935305676L, 
-888219960L, 1351194298L, -1603837808L, 54000364L, -1245599148L, 
-1797271918L, 1779333920L, -1904022580L, -1882981152L, 1265003202L, 
871964712L, 290884524L, 719755964L, 1515439602L, 1573234224L, 
-1039552604L, -316373576L, -2096316582L, 563206288L, -1978840196L, 
-454221036L, -1594195646L, 892125952L, -44970052L, 534901584L, 
1234646306L, -628597176L, -1839798260L, 390598748L, -1654048302L, 
-309052928L, 550781268L, 400483560L, -2069594886L, -1083083056L, 
-1955680980L, -1587951372L, -743041646L, 1464826208L, 1093023756L, 
639467232L, 1540479586L, 1063272104L, 549656140L, -2099176580L, 
1149433138L, 1129045616L, -1919820988L, 964391896L, -1626898566L, 
972739120L, -1365832772L, -2010914220L, -1540576190L, -1313277792L, 
-852622660L, -1229201968L, -1756014750L, -1251880216L, 170943692L, 
-2010173956L, 458873330L, 408548992L, -1057421836L, -80661880L, 
1370785594L, 1082580816L, -745042836L, -1066201708L, -540300206L, 
-2134378016L, -996297524L, -2033438560L, 1186277762L, -1109526296L, 
-1673370900L, 1045337404L, 1544866930L, 1038984304L, -1016009692L, 
-78569416L, 1838803738L, 705797328L, 1971575612L, -248764524L, 
988131202L, 543569344L, -82558404L, 1263991184L, -660997598L, 
-551850744L, 901429900L, 655939356L, 1973563538L, 1680482688L, 
1311275972L, -1374066955L, 941634399L, -675717704L, 897475734L, 
996301475L, 838057061L, -710153854L, -734776592L, -1303239015L, 
698002059L, 1157884796L, 111365546L, 704423391L, -534826135L, 
-1524206722L, 1392798572L, -1473634371L, -1051169657L, -1713299344L, 
547441422L, 2037573691L, -2036504803L, 610403786L, 492961512L, 
-259550767L, 1474044867L, 2015624388L, -1716632718L, -288612793L, 
1744420369L, -1206595690L, -693456140L, -905038715L, 1783245263L, 
-2076263224L, 415509862L, -2140827757L, 1592479989L, 1055840466L, 
824567584L, -741587191L, -2125893573L, 20735372L, -1684693990L, 
-2131799921L, -462346279L, -1238562706L, 150440956L, 2094717293L, 
1650815767L, -1101057312L, -964857666L, 1371012747L, 2058183949L, 
-910366406L, -1234650760L, -496600287L, 1424960467L, 1159582452L, 
-1430029438L, -317822441L, 2044864737L, 2021680678L, -862945500L, 
577613781L, 142440319L, 141672984L, 1664532790L, -1900105789L, 
-1303982587L, -1633722974L, -1393466992L, -935724743L, 934772203L, 
-829489444L, 443534090L, 624223743L, -1777189815L, -1387561506L, 
-920105588L, 1532270941L, -248606361L, 1977946768L, 1542155438L, 
-1691566757L, -1208827587L, -1969304662L, 789077064L, -1341455247L, 
-1049030173L, 1724263652L, -318416878L, 72268775L, -1089952847L, 
1417698806L, -1938694124L, 698429221L, -1593354193L, -1619197464L, 
-1823508794L, 2086541171L, -1199165483L, -176601166L, -2144910976L, 
-1319175831L, -1834119717L, 2080310572L, 946007866L, -547569169L, 
289494137L, -1225267442L, -1111891236L, -180117171L, 214367415L, 
591406656L, 191555358L, -1630388757L, 752173421L, -228548646L, 
-591795816L, -2106378239L, -1481082445L, 1552634068L, -1047120414L, 
1977016439L, 1018545601L, -582832314L, -289957244L, -1064590283L, 
-105774049L, 1398539128L, -479663530L, -837699869L, -691638107L, 
-1489051326L, -289593552L, 1855468377L, 1886468299L, 634194236L, 
-71613078L, -1915990113L, 531823145L, -1534155586L, 807173548L, 
-51535107L, 2079673927L, 1651387696L, -1195368882L, 636907131L, 
125910493L, 1870429194L, -2076992216L, 1504793489L, 382756227L, 
1745977988L, 1457414322L, -1496162297L, -1920910255L, 97094614L, 
1863897780L, 1897419461L, -216886257L, -815541112L, 657175974L, 
659414355L, -622463307L, 1890774034L, -940151328L, 913026209L
)
pedroaraujo9/caiman2 documentation built on May 28, 2019, 12:03 p.m.