I want to use a single explanatory variable in a data frame to explain changes in a number of other variables. The data frame looks something like:
df:
explanatory_var dep_var_1 dep_var_2 dep_var_3
1 1.05 1.75 1.98
7 3.8 2.1 9.5
4.5 2 1.9 4
in pseudo code, I want to:
fit_df$coefficient <- lm((dep_var_1, dep_var_2, dep_var_3) ~ explanatory_variable, na.action = na.exclude)$coefficient
fit_df$intercept <- lm((dep_var_1, dep_var_2, dep_var_3) ~ explanatory_variable, na.action = na.exclude)$intercept
fit_df$coefficient_significance_code <- lm((dep_var_1, dep_var_2, dep_var_3) ~ explanatory_variable, na.action = na.exclude)$coefficient_significance_code
fit_df$intercept_significance_code <- lm((dep_var_1, dep_var_2, dep_var_3) ~ explanatory_variable, na.action = na.exclude)$intercept_significance_code
so that I end up with something like (data totally made up, doesn't fit the above, just an example)
fit_df:
variable coefficient intercept coefficient_significance_code intercept_significance_code
dep_var_1 .35 0.5 *** ***
dep_var_2 .5 0.75 *** ***
dep_var_3 .43 1.0 *** ***
I have what I believe to be the opposite of this question: Using R's lm on a dataframe with a list of predictors
The answer seems like it is probably related to this: Repeat regression with varying dependent variable , but I am not creating my data frame, nor am I looking for an ls mean.
if you are using a single explanatory variable to explain changes in multiple dependent variables, a manova might be more appropriate for the task.
Using your data, it might go something like this:
exp_var <- c(1, 7, 4.5)
dep_var_1 <- c(1.05, 3.8, 2)
dep_var_2 <- c(1.75, 2.1, 1.9)
dep_var_3 <- c(1.98, 9.5, 4)
df <- data.frame(exp_var, dep_var_1, dep_var_2, dep_var_3)
model <- manova(cbind(dep_var_1,dep_var_2,dep_var_3) ~ exp_var, data = df)
summary(model)
By the way, there is not enough observations to run that code and it will result in an error. I hope that your dataset has plenty of observations. Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.