0
我想計算一個適合R的模型的擬合值,使用glm
對MySQL表中的數據進行處理,並將結果返回到該表中,我怎麼能這樣做?將R模型的擬合值插入到MySQL表中
我想計算一個適合R的模型的擬合值,使用glm
對MySQL表中的數據進行處理,並將結果返回到該表中,我怎麼能這樣做?將R模型的擬合值插入到MySQL表中
# preparation
library("RMySQL")
con <- dbConnect(MySQL(), user="####", pass="###", host="127.0.0.1", db="###")
on.exit(dbDisconnect(con))
# fetching the data and calculating fit
tab <- dbGetQuery(con,"SELECT ID, dep, indep1, indep2 FROM table WHERE !(ISNULL(ID) OR ISNULL(dep) OR ISNULL(indep1) OR ISNULL(indep2))")
regression = glm(tab$dep ~ tab$indep1 + tab$indep2, gaussian)
# preparing data for insertion
insert <- data.frame(tab$ID, fitted.values(regression)
colnames(insert) <- c('ID', 'dep')
# table hassle (inserting data, combining with previous table, deleting old and fitresult renaming combined back to original name
if (dbExistsTable(con, '_result') {
dbRemoveTable(con, '_result');
}
dbWriteTable(con, '_result', insert)
dbSendQuery(con, 'CREATE TABLE temporary SELECT table.*, _result.dep FROM table LEFT JOIN result USING (ID)')
dbRemoveTable(con, 'table')
dbRemoveTable(con, '_result')
dbSendQuery(con, 'RENAME TABLE temporary TO table')
我想這可能是因爲經常interessing PPL要麼知道的MySQL _or_ [R – Hoffmann 2013-05-13 14:41:37
你要保存的預測值?迴歸係數? – Dason 2013-05-13 14:57:10
我想保存預測(擬合)的值 – Hoffmann 2013-05-13 14:58:13