1、问题
假定有下面的数据,希望对该数据进行回归分析:
ID | score1 | score2 | Gender |
101 | 11.35321 | 0.2 | male |
102 | 12.58654 | 0.6 | male |
103 | 19.7938 | 0.9 | female |
104 | 12.01352 | 0.4 | female |
105 | 11.97081 | 0.4 | female |
106 | 17.3748 | 0.7 | female |
107 | 14.24884 | 0.3 | female |
108 | 18.47972 | 0.8 | male |
109 | 18.74792 | 0.8 | male |
110 | 18.90146 | 0.7 | male |
或者是当gender为male时,score1对score2的回归
2、解决
可以用which(也可以用subset):
1、想做当gender 为male时, summary (score1)
>summary(score[which(score$Gender=='male'),'score1'])
注:score为包含楼主示例数据的数据框名。
2、或者是当gender为male时,score1对score2的回归
>score1<-score[which(score$Gender=='male'),'score1']
这是语句2(公式编辑器真是很讨厌,总是弄坏版面,非得用文字隔开):
>score2<-score[which(score$Gender=='male'),'score2']
>score.lm<-lm(score1~score2)
或更简单的:
>score.lm<-lm(score1~score2, data=score[which(score$Gender=='male'),])