# README.md In CosmoPhotoz: Photometric redshift estimation using generalized linear models

title: "Photometric Redshift with CosmoPhotoz" authors: Rafael S. de Souza, Jonny Elliot, Alberto Krone-Martins, Émille Ishida, Joseph Hilbe output: html_document runtime: shiny

This is a short tutorial explaining how to perform photometric redshift estimation using the CosmoPhotoz R package.

# Required libraries

```{r,results='hide',message=FALSE, cache=FALSE} require(CosmoPhotoz) require(ggplot2)

``````
Load the PHAT0 data included in the package. Here we are using 5% of all dataset for training.

```{r}
data(PHAT0train)

data(PHAT0test)
``````
``````PC_comb<-computeCombPCA(subset(PHAT0train,select=c(-redshift)),
subset(PHAT0test,select=c(-redshift)))
``````

Number of variance explained by each PC

``````PC_comb\$PCsum
``````

Add the redshift column to the PCA projections of the Training sample

``````Trainpc<-cbind(PC_comb\$x,redshift=PHAT0train\$redshift)

``````

Store the PCA projections for the testing sample in the vector Testpc

```{r, echo=FALSE} Testpc<-PC_comb\$y

``````

Train  the glm model using Gamma Family. 6 PCs explain 99.5% of data variance. In order to account for small variations in the shape, we include a polynomial term for the 2 first PCs (95% of data variance)

```{r}

Fit<-glmTrainPhotoZ(Trainpc,formula=redshift~poly(Comp.1,2)*poly(Comp.2,2)*Comp.3*Comp.4*Comp.5*Comp.6,method="Bayesian",family="gamma")

``````

Once we fit our GLM model, we can predict the redshift for the "photometric" sample

```{r, echo=FALSE}

photoz<-predict(Fit\$glmfit,newdata = Testpc,type="response")

``````
Store the redshift from the testing sample in the vector specz for comparison

```{r, echo=FALSE}
specz<-PHAT0test\$redshift
``````

Compute basic diagnostic statistics

```{r, echo=FALSE} computeDiagPhotoZ(photoz, specz)

``````

Create basic diagnostic   plots

Kernel density distribution of the full scatter
\$(specz-photoz)/(1+specz)\$

```{r,fig.width=8, fig.height=9}
plotDiagPhotoZ(photoz, specz, type = "errordist")

``````

Predicted vs Actuall values Select 15,000 points to show

``````datashow<-sample(length(photoz),15000)
``````

```{r,fig.width=8, fig.height=9} plotDiagPhotoZ(photoz[datashow], specz[datashow], type = "predobs")+coord_cartesian(xlim =c(0,1.5), ylim = c(0,1.5))

``````

Scatter distribution as a function of redshift, violin plot

```{r,fig.width=12, fig.height=9}
plotDiagPhotoZ(photoz, specz, type = "errorviolins")
``````

Scatter distribution as a function of redshift, box plot

```{r,fig.width=12, fig.height=9} plotDiagPhotoZ(photoz, specz, type = "box")

``````

```{r, echo=FALSE}
shinyAppDir("paste(find.package("CosmoPhotoz"),"/glmPhotoZ-2/",sep=""))
``````

## Try the CosmoPhotoz package in your browser

Any scripts or data that you put into this service are public.

CosmoPhotoz documentation built on May 29, 2017, 3:59 p.m.
{"ft_lang_label":"__label__en","ft_lang_prob":0.5800478,"math_prob":0.97171146,"size":2615,"snap":"2022-05-2022-21","text_gpt3_token_len":776,"char_repetition_ratio":0.12102643,"word_repetition_ratio":0.05923345,"special_character_ratio":0.2500956,"punctuation_ratio":0.17113402,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98836887,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-05-18T04:32:06Z\",\"WARC-Record-ID\":\"<urn:uuid:7debf192-5655-41ac-a943-44ab6cddd63f>\",\"Content-Length\":\"23492\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:40768a19-9054-4613-9a26-67161d179ba8>\",\"WARC-Concurrent-To\":\"<urn:uuid:68202475-c355-4ba7-80d7-2f232c73481a>\",\"WARC-IP-Address\":\"51.81.83.12\",\"WARC-Target-URI\":\"https://rdrr.io/cran/CosmoPhotoz/f/README.md\",\"WARC-Payload-Digest\":\"sha1:6VYN5RHZZIFBTLUZRXM2WK4B6H34TAQR\",\"WARC-Block-Digest\":\"sha1:ZNQ5W57FCFHQOTIZU3MAXOW46ZC5FFME\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-21/CC-MAIN-2022-21_segments_1652662521041.0_warc_CC-MAIN-20220518021247-20220518051247-00634.warc.gz\"}"}