具有多个条件的R条件回归 [英] R Conditional Regression with Multiple Conditions

查看:231
本文介绍了具有多个条件的R条件回归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试基于两个条件在R中进行回归.我的数据具有年份和其他分类的二进制变量.仅使用1个条件,我就可以使回归正常运行:

I am trying to run a regression in R based on two conditions. My data has binary variables for both year and another classification. I can get the regression to run properly while only using 1 condition:

# now time for the millions of OLS
# format: OLSABCD where ABCD are binary for the values of MSA/UA and years
# A = 1 if MSA, 0 if UA
# B = 1 if 2010
# C = 1 if 2000
# D = 1 if 1990

OLS1000<-summary(lm(lnrank ~ lnpop, data = subset(df, msa==1)))
OLS1000

但是,我无法弄清楚如何同时将MSA/UA分类与年份变量一起使用.我已经尝试过:

However I cannot figure out how to get both the MSA/UA classification to work with the year variables as well. I have tried:

OLS1100<-summary(lm(lnrank ~ lnpop, data = subset(df, msa==1, df$2010==1)))
OLS1100

但是它返回错误:

Error: unexpected numeric constant in "OLS1100<-summary(lm(lnrank ~ lnpop,   
data = subset(df, msa==1, df$2010"

如何使程序在两种情况下都能运行?

How can I get the program to run utilizing both conditions?

再次感谢您!

推荐答案

问题是:

df$2010

如果您的数据确实有一个名为2010的列,那么您需要在其周围加上反引号:

If your data really has a column named 2010, then you need backticks around it:

df$`2010`

在您的子集中,不要两次指定df:

And in your subset, don't specify df twice:

subset(df, msa == 1, `2010` == 1)

通常,最好不要使用数字开头的列名.最好不要命名数据帧df,因为这是一个函数名.

In general it's better if column names don't start with digits. It's also best not to name data frames df, since that's a function name.

这篇关于具有多个条件的R条件回归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆