2017-01-16 14 views
0
variables.null.model <- paste('utalter', 'lcsex', 'utcigreg', 'utbmi', 'month', sep = '+') 
variables.full.model <- paste('utalter', 'lcsex', 'utcigreg', 'utbmi', 'month', 'ltedyrs','occ_status', 'marital_status', 'social_cat','GC_linc125_07', 'GC_linc250_07', 'GC_linc500_07', 'GC_linc1000_07', 'GC_linc5000_07', 'GC_pop500_08','utalkkon', 'activity', 'utpyrs', 'cvd', 'utmstati', 'utmfibra', 'utantihy', 'utmeddia', 'utmadins','utwhrat','ul_choln', sep='+') 
pollutants_3 <- c('GC_PM10_09', 'GC_PM25_09', 'GC_Coarse_09', 'GC_BS25_09', 'GC_NOX_09', '$GC_NO2_09') 

null <- paste(variables.null.model, pollutants_3, sep='+') 
full <- paste(variables.full.model, pollutants_3, sep='+') 

fun.model.summary <- function(x) { 
formula <- as.formula(paste("log_sfrp5 ~", x)) 
lm <- lm(formula, data = kalonji.na) 
coef(summary(lm)) 
} 

lm.summary <- lapply(full, fun.model.summary) 

私はいくつかの大気汚染データに取り組んでおり、線形回帰関数を実行して係数を要約したいと考えています。上記のコードがありますが、このエラーが発生します:線形回帰関数の不具合

Error in parse(text = x, keep.source = FALSE) : :1:269: unexpected '$'

私はこれをどのように修正できますか?

+0

「pollutants_3」とは何ですか? 'lapply(c(null、full)、...)'は動作するはずです –

+0

あなたの 'full'変数は長さ1の文字ベクトルです。なぜそれに' lapply'を使いたいのですか? – Istrel

+0

@Istrelもう一度見てください、それは長さ> 1を持っています。 –

答えて

0

あなたの最後の汚染物質は'$GC_NO2_09'です。浮遊$記号に注意してください。

しかし、私がコメントで言ったように、ここでは文字列を使用しないことを強くお勧めします。。文字列をas.nameでR識別子に変換することによって、Rオブジェクトから直接式を構築します。

Reducecallを使用すると、名前の一覧を合計で組み合わせることができます。例えば:背景のビットについて

make_addition = function (lhs, rhs) 
    call('+', lhs, rhs) 

variables_null_model = c('utalter', 'lcsex', 'utcigreg', 'utbmi', 'month') 
interaction_terms_full_model = Reduce(make_addition, lapply(variables_null_model, as.name)) 

fun_model_summary = function (x) { 
    formula = call('~', quote(log_sfrp5), call('+', interaction_terms_full_model, as.name(x))) 
    lm = lm(formula, data = kalonji_na) 
    coef(summary(lm)) 
} 

lm_summary = lapply(pollutants_3, fun_model_summary) 

、ここで文字列を使用して型システムを覆すと型なし文字列によって適切な、異なるタイプを置き換えます。これはstringly typingとして知られており、バグを隠すのでアンチパターンです。あなたの質問はそのようなバグの例です。

+0

あなたの助けに感謝の男...今、光の中にいる – Dee