2017年4月28日 星期五

melt() 使用時 measure variable 的型態要一致

使用 melt() 來 reshape 資料時, measure variable 的型態要一致,否則會出現警告。

例如有以下的資料:
# Parts of Codes are from https://www.r-bloggers.com/reshape-and-aggregate-data-with-the-r-package-reshape2/
require(reshape2)
x <- data.frame(subject = c("John", "Mary"),
SN = c(1,2),
time = c(1,1),
age = c(33,NA),
weight = c(90, NA),
height = c(2,2))
melt(x,id.vars = "SN")
str(x)
view raw melt_trap.r hosted with ❤ by GitHub


資料中,subject 欄位為 factor。若使用 subject 為 id.var 進行 melt,則會出現以下警告訊息:


因為,第一個欄位為 Factor 型態,melt() 把後面其他的欄位也視為 Factor,造成轉換時的錯誤。


Reference:
[1] thiagogm, 2013. Reshape and aggregate data with the R package reshape2, www.r-bloggers.com.

2017年4月27日 星期四

ui:debug 和 flash.keep 的衝突

若在 facelet 中使用 ui:debug,則在同一個 facelet 中使用 flash.keep 時無法正常顯示 flash scope 中的變數。

測試資料
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="2">
Name <h:inputText value="#{flash.keep.name}" />
Tel: <h:inputText value="#{flash.keep.tel}" />
</h:panelGrid>
<h:commandButton value="Back" action="index?faces-redirect=true" />
</h:form>
</h:body>
</html>
view raw confirm.xhtml hosted with ❤ by GitHub
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<ui:debug hotkey="x" />
<h:panelGrid columns="2">
Name <h:inputText value="#{flash.name}" />
Tel: <h:inputText value="#{flash.tel}" />
</h:panelGrid>
<h:commandButton value="Submit" action="confirm?faces-redirect=true" />
</h:form>
</h:body>
</html>
view raw index.xhtml hosted with ❤ by GitHub

References:
[1] ui:debug
[2] Flash class

2017年4月15日 星期六

Expectation-Maximization (EM) Algorithm 介紹


演算法,用來計算混合模型(Mixture Model)下的 Maximum Likelyhood 估計。

混合模型(Mixture Model):
觀察到的資料點(Data Point) 來自於兩個分配(Distribution), 哪些資料點是屬於那個分配呢?




Youtube 影片介紹
EM Algorithm: How it works (1) by Victor Lavrenco
EM Algorithm: How it words (2) by Victor Lavrenco