-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlin_regression.R
More file actions
105 lines (75 loc) · 2.21 KB
/
lin_regression.R
File metadata and controls
105 lines (75 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
data <- read.csv(file = 'CommViolPredUnnormalizedData.csv', stringsAsFactors=FALSE)
data$Community.name <- NULL
data$state <- NULL
for(c in colnames(data)){
col <- data[[c]]
for(i in 1:length(col)) {
col[i] <- as.numeric(as.character(col[i]))
}
col_d <- as.double(as.character(col))
col_withot_na <- na.omit(col_d)
m <- mean(col_withot_na)
for(i in 1:length(col)) {
if(is.na(col[i])){
col[i] <- m
}
}
col <- as.double(as.character(col))
data[[c]] <- col
}
data
dataFiltered <- data[,c("medIncome","PctPopUnderPov","PctUnemployed","ViolentCrimesPerPop","nonViolPerPop")]
dataFiltered
attach(dataFiltered)
#Violent Crimes
multi.fit = lm(ViolentCrimesPerPop~medIncome+PctPopUnderPov+PctUnemployed)
summary(multi.fit)
multi.fit = lm(ViolentCrimesPerPop~PctPopUnderPov+PctUnemployed)
summary(multi.fit)
multi.fit = lm(ViolentCrimesPerPop~PctUnemployed)
summary(multi.fit)
multi.fit = lm(ViolentCrimesPerPop~PctPopUnderPov)
summary(multi.fit)
res<-scale( multi.fit$residuals )
mean(multi.fit$residuals)
sum(multi.fit$residuals)
dwtest(multi.fit)
bptest(multi.fit)
layout(matrix(c(1,2,3,4),2,2,byrow=T))
plot(multi.fit$fitted.values,rstandard(multi.fit),
main = "Multi Fit Standarized Residuals",
xlab="Predictions",
ylab="Standarized Residuals",
ylim = c(-2.5,2.5))
abline(h=0,lty=2)
plot(ViolentCrimesPerPop,res)
abline(h=0,lty=2)
hist(res,main="Histograma de Residuos")
qqnorm(res)
qqline(res)
#Non-Violent Crimes
multi.fit = lm(nonViolPerPop~medIncome+PctPopUnderPov+PctUnemployed)
summary(multi.fit)
multi.fit = lm(nonViolPerPop~PctPopUnderPov+PctUnemployed)
summary(multi.fit)
multi.fit = lm(nonViolPerPop~PctUnemployed)
summary(multi.fit)
multi.fit = lm(nonViolPerPop~PctPopUnderPov)
summary(multi.fit)
res<-scale( multi.fit$residuals )
mean(multi.fit$residuals)
sum(multi.fit$residuals)
dwtest(multi.fit)
bptest(multi.fit)
layout(matrix(c(1,2,3,4),2,2,byrow=T))
plot(multi.fit$fitted.values,rstandard(multi.fit),
main = "Multi Fit Standarized Residuals",
xlab="Predictions",
ylab="Standarized Residuals",
ylim = c(-2.5,2.5))
abline(h=0,lty=2)
plot(nonViolPerPop,res)
abline(h=0,lty=2)
hist(res,main="Histograma de Residuos")
qqnorm(res)
qqline(res)