Regress each dependent variable ( dep_var ) against independent variable ( ind_var )
I am trying to perform linear regressions for multiple dependent variables against a independent variable one at a time.
When there is a missing observation (NA) , the entire row is not used for that particular regression.
I have done it by looping/iterating through each column of dependent variable.
fit = list()
for( i in 1 : 2 ) {
fit[[i]] = lm( mydf$Ind_Var[ which( !is.na( mydf[ , (2+i) ] ) ) ] ~ na.omit( mydf[ , (2+i) ] ) )
}
Without having to involve other packages ( let's restrict to functions like lm, apply family functions , do/do.call), how can I do so?
Random Data
mydf = data.frame(
"ID" = rep( "A" , 25 ),
"Date" = c( 1 : 25 ),
"Dep_1" = c( 0.78670185, 0.15221561, NA, 0.85270392, 0.90057399, 0.75974473, 0.42026760, 0.64035871, 0.83012434, 0.04985492, 0.06619375, 0.36024745, 0.83969627, 0.45293842, 0.25272036, NA, 0.63783321, 0.42294695, 0.06726004, 0.14124547, 0.54590193, 0.99560087, 0.14255501, 0.41559977, 0.80120970) ,
"Dep_2" = c( 0.736137983, 0.979317444, 0.901380500, 0.942325049, 0.420741297, NA, 0.243408607, 0.824064331, 0.462912557, NA, 0.710834065, 0.264922818, 0.797917063, 0.578866651, 0.955944058, 0.291149075, 0.437322581, 0.298153168, 0.579299049, 0.671718144, 0.545720702, 0.099175216, 0.808933227, 0.912825535, 0.417438973 ) ,
"Ind_Var" = c( 75:51 ) )
My own attempt of converting will be:
apply( mydf[ ,-c(1:2) ] , 2 , function( x ) lm( mydf$Ind_Var[ which( !is.na( x ) ) ] ~ na.omit(x) ) )
but this involves having mydf hardcoded.
I apologize if I have used any incorrect terms.
foreachdoesn't look like a base function.foreachto create the list, but I have edited it tofornow for consistency.