转换频率数据以用于R中的逻辑回归

时间:2019-12-10 03:58:47

标签: r dataframe tidyverse logistic-regression

一个简单的问题:我有以下数据,我需要以一种可以对其进行逻辑回归的格式来获取数据。

df = pd.DataFrame(columns=['Q1'], index=range(3))
df['Q1'][0]=[{'id':'1-1', 'content': 'ABC'}, {'id':'1-3', 'content': 'XYZ'}]  
df['Q1'][1]=[{'id':'1-1', 'content': 'ABC'}]
df['Q1'][2]=[{'id':'1-1', 'content': 'ABC'}, {'id':'1-3', 'content': 'XYZ'}, {'id': '1-5', 'content': 'PQR'}]


a = df.groupby(np.arange(len(df))//len(df)).agg(list)['Q1'][0]

def getcontent(x): 
   clist = [] 
   for item in x[0]: 
      if item != None: 
         clist.append(item['content']) 
   return clist 

temp = pd.DataFrame(a)
temp.T.groupby(np.arange(len(temp.T))//len(temp.T)).agg(list).apply(getcontent) 
df['Q1'] = temp.T.groupby(np.arange(len(temp.T))//len(temp.T)).agg(list).apply(getcontent)

它看起来像这样:

                Q1
0       [ABC, XYZ]
1            [ABC]
2  [ABC, XYZ, PQR]

我需要进行逻辑回归以预测pvp的压力。通常,我认为我只需要使用tidyverse中的pvp <- rep(c("lib", "mod", "con"), 3) pres <- c(rep("Bush", 3), rep("Clinton", 3), rep("Perot", 3)) count <- c(70, 195, 382, 324, 332, 199, 56, 101, 117) df <- as.data.frame(cbind(pvp, pres, count)) df$pres <- recode(df$pres, 'Clinton' = '1', 'Bush' = '0', 'Perot' = '0') df$count <- as.numeric(as.character(df$count)) 即可将数据转换为宽格式。但是在这里,我在扩展函数中使用key = pvp有一个问题。我不能折叠类别,因为其中一些类别显然对应于pres = 1,而某些类别对应于pres =0。我可以使用什么解决方案以一种可以对它进行逻辑回归的格式获取数据?

谢谢。

1 个答案:

答案 0 :(得分:1)

不需要扩展数据,可以在训练模型时使用“ weight”参数。

main()
{
    FOO<int, MYVEC<std::string>> obj2; // Probably fails to compile with a cryptic message
    FOO<int, MYVEC<short>> obj3; // Hopefully fails to compile, could easily have undefined behaviour
}
相关问题