---
title: "TP_Chapitre_3.2"
output: html_document
date: "2025-01-28"
---

```{r, include=FALSE}
knitr::opts_chunk$set(echo = TRUE,comment='',class.source="bg-info", class.output="bg-success")
```


# Functional case

# Fonctions 

```{r}
library(sm)
source('npfda.R')

#### 
ROC=function(z,p1)
{
  p=sort(unique(p1))
  lp=length(p)
  ROC=matrix(NA,lp,2)
  for (s in 1:lp)
  {
    ROC[s,1]=mean(p1[z==0]>p[s])
    ROC[s,2]=mean(p1[z==1]>p[s])  
  }
  AUC=sum(ROC[-lp,2]*(ROC[-lp,1]-ROC[-1,1]))
  
  colnames(ROC)=c("FPR","TPR")
  plot(ROC,type='l',main=paste('AUC =', AUC))
  return(list(ROC=ROC,AUC=AUC))
  }

#######
Classif_NP_fun=function(X,Y,X0=NA,...)
{
  X=as.matrix(X)
  n=length(Y)
  V=sort(unique(Y))
  n_V=length(V)
  Prob=matrix(NA,n,n_V)
  colnames(Prob)=V
  Class=rep(NA,n)
  if (!is.na(max(X0)))
  {
    X0=as.matrix(X0)
    P0=matrix(NA,nrow(X0),n_V)
    Class0=rep(NA,n)
  }
  for (v in 1:n_V)
  {
    z=as.numeric(Y==V[v])
    X1=X
    if (!is.na(max(X0))) {X1=X0}
    re=funopare.kernel.cv(z,X,X1,...)
    Prob[,v]=re$Estimated.values
    if (!is.na(max(X0))) {P0[,v]=re$Predicted.values}
  }
  if (n_V==2) {ROC(Y==V[2],Prob[,2])}
  Class=V[apply(Prob,1,which.max)]
  M_table=table(Y,Class)
  if (!is.na(max(X0))) {Class0=V[apply(P0,1,which.max)]}
  if (!is.na(max(X0))) {return(list(Class=Class, Prob=Prob, M_table=M_table, err=1-sum(diag(M_table))/n, Class0=Class0,Prob0=P0))}
  else {return(list(Class=Class, Prob=Prob, M_table=M_table, err=1-sum(diag(M_table))/n))}
}

```

# Regression

## Real world data

```{r}
#tec=read.table('npfda-spectrometric.dat')
#X=tec[,1:100]
#Y=tec[,101]
load("Tecator.RData")
matplot(t(X),type='l')
```

```{r}
par(mfrow=c(3,3))
for (j in 0:8)
{
re=funopare.kernel.cv(Y,X,X,q=j,nknot=10,range.grid=c(0,1))
plot(re$Estimated.values, Y)
title(main=paste('q=',j,' et CV=',signif(re$Mse,7),sep=''))
abline(0,1)
}
```

```{r}
X=as.matrix(X)
par(mfrow=c(3,3))
for (j in 0:8)
{
re=funopare.kernel.cv(Y,X,X,semimetric='pca',qv=2:(j+2))
plot(re$Estimated.values, Y)
title(main=paste('qv=2:',j+2,' et CV=',signif(re$Mse,7),sep=''))
abline(0,1)
}
```

## Classification

```{r}
load("PHONEMES.RData")
learn=1:1000
test=1001:2000

X_learn=CURVES[learn,]
Y_learn=PHONEME[learn]
X_test=CURVES[test,]
Y_test=PHONEME[test]

par(mfrow=c(2,3))
ph=sort(unique(Y_learn))
for (j in ph) {matplot(t(X_learn[Y_learn==j,]),type='l',main=j)}
```
```{r}


for (j in 1:10)
{
C_re=Classif_NP_fun(X_learn,Y_learn,X0=X_test,semimetric='mplsr', q=j)
cat(paste('q=',j,'   \n',sep=''))
cat('Qualité de la classification sur l\'échantillon test \n')
cat('matrice de confusion: ')
M0=table(Y_test, C_re$Class0)
err0=1-sum(diag(M0))/sum(M0)
print(M0)
cat('erreur:')
cat(err0)
cat('   \n')
cat('   \n')

}
```

```{r}


for (j in 1:10)
{
C_re=Classif_NP_fun(X_learn,Y_learn,X0=X_test,semimetric='pca', q=j)
cat(paste('q=',j,'   \n',sep=''))
cat('Qualité de la classification sur l\'échantillon test \n')
cat('matrice de confusion: ')
M0=table(Y_test, C_re$Class0)
err0=1-sum(diag(M0))/sum(M0)
print(M0)
cat('erreur:')
cat(err0)
cat('   \n')
cat('   \n')

}
```