1. a)

data<-read.table("hospital.txt", sep="\t", dec=".", header=TRUE)
head(data)
##   ID  Stay  Age InfctRsk Culture  Xray Beds MedSchool Region Census Nurses
## 1  5 11.20 56.5      5.7    34.5  88.9  180         2      1    134    151
## 2 10  8.84 56.3      6.3    29.6  82.6   85         2      1     59     66
## 3 11 11.07 53.2      4.9    28.5 122.0  768         1      1    591    656
## 4 13 12.78 56.8      7.7    46.0 116.9  322         1      1    252    349
## 5 18 11.62 53.9      6.4    25.5  99.2  133         2      1    113    101
## 6 23  9.78 52.3      5.0    17.6  95.9  270         1      1    240    198
##   Facilities
## 1       40.0
## 2       40.0
## 3       80.0
## 4       57.1
## 5       37.1
## 6       57.1
I<-data$InfctRsk
S<-data$Stay
a<-lm(I ~ S)
plot(S, I)
abline(a)

1. b)

summary(a)
## 
## Call:
## lm(formula = I ~ S)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.6145 -0.4660  0.1388  0.4970  2.4310 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -1.15982    0.95580  -1.213     0.23    
## S            0.56887    0.09416   6.041  1.3e-07 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.024 on 56 degrees of freedom
## Multiple R-squared:  0.3946, Adjusted R-squared:  0.3838 
## F-statistic:  36.5 on 1 and 56 DF,  p-value: 1.302e-07

Intercept estimate=-1.15982, std.error= 0.95580

If you would be 0 days in hospital infection risk would be -1.16% but 0 days in hospital is not realistic. Every day that you are in hospital risk increases by 0.57%.

1. c)

confint(a)
##                  2.5 %    97.5 %
## (Intercept) -3.0745094 0.7548714
## S            0.3802334 0.7574995

{0.3802334, 0.7574995}

We can tell there is significant statistical linear connection between days in hospital and infection risk.

1. d)

summary(a)$sigma^2
## [1] 1.049577

=1.049577

2. a)

Data<-read.table("muscle.txt", sep="\t", dec=".", header=FALSE)
DATA <- read.table(text = Data$V1, header = FALSE)
head(DATA)
##    V1 V2
## 1 106 43
## 2 106 41
## 3  97 47
## 4 113 46
## 5  96 45
## 6 119 41
hist(DATA$V2)

hist(DATA$V1)

boxplot(DATA$V2)

boxplot(DATA$V1)

b<-lm(V1 ~ V2, data=DATA)
plot(DATA$V2, DATA$V1)
abline(b)

2. b)

coef(b)
## (Intercept)          V2 
##  156.346564   -1.189996

´=-1.189996 One year decreases muscle mass 1.19 units.

156.346564-1.189996*60
## [1] 84.9468

Estimate for muscle mass for women at 60 is 84.95

residuals(b)[8]
##        8 
## 4.443252

=4.443252

2.c)

predict(b,
        newdata=data.frame(V2=60),
        interval="confidence",
        level=0.95)
##        fit      lwr      upr
## 1 84.94683 82.83471 87.05895

=84.95 82.83 87.06

predict(b,
        newdata=data.frame(V2=60),
        interval="prediction",
        level=0.95)
##        fit      lwr     upr
## 1 84.94683 68.45067 101.443

=84.95 68.45 101.44

2. d)

summary(b)
## 
## Call:
## lm(formula = V1 ~ V2, data = DATA)
## 
## Residuals:
##      Min       1Q   Median       3Q      Max 
## -16.1368  -6.1968  -0.5969   6.7607  23.4731 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 156.3466     5.5123   28.36   <2e-16 ***
## V2           -1.1900     0.0902  -13.19   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 8.173 on 58 degrees of freedom
## Multiple R-squared:  0.7501, Adjusted R-squared:  0.7458 
## F-statistic: 174.1 on 1 and 58 DF,  p-value: < 2.2e-16

There is statistically significant evidence of a negative linear association between age and muscle mass at the 5% significance level.

2. e)

par(mfrow = c(2,2))
plot(b)

3. a)

data3<-read.table("Stopdist.txt", sep="\t", dec=".", header=TRUE)
head(data3)
##   StopDist Speed
## 1        4     4
## 2        2     5
## 3        8     5
## 4        8     5
## 5        4     5
## 6        6     7
c <- lm(StopDist ~ Speed, data = data3)
summary(c)
## 
## Call:
## lm(formula = StopDist ~ Speed, data = data3)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -25.141  -7.300  -2.141   6.044  35.946 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) -20.2734     3.2384   -6.26 4.25e-08 ***
## Speed         3.1366     0.1517   20.68  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 11.8 on 61 degrees of freedom
## Multiple R-squared:  0.8752, Adjusted R-squared:  0.8731 
## F-statistic: 427.7 on 1 and 61 DF,  p-value: < 2.2e-16
plot(data3$Speed, data3$StopDist)
abline(c)

3. b)

par(mfrow = c(2,2))
plot(c)

3. c)

data3$StopDist <- sqrt(data3$StopDist)
d<-lm(StopDist~Speed, data=data3)
summary(d)
## 
## Call:
## lm(formula = StopDist ~ Speed, data = data3)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -1.4879 -0.5487  0.0098  0.5291  1.5545 
## 
## Coefficients:
##             Estimate Std. Error t value Pr(>|t|)    
## (Intercept) 0.918283   0.197406   4.652 1.82e-05 ***
## Speed       0.252568   0.009246  27.317  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 0.7193 on 61 degrees of freedom
## Multiple R-squared:  0.9244, Adjusted R-squared:  0.9232 
## F-statistic: 746.2 on 1 and 61 DF,  p-value: < 2.2e-16
par(mfrow = c(2,2))
plot(d)

3. d)

newdata <- data.frame(Speed = seq(10, 50, by = 10))
ennustus <- predict(d,
                newdata,
                interval = "prediction",
                level = 0.95)
ennustus
##         fit       lwr       upr
## 1  3.443966  1.984875  4.903057
## 2  5.969649  4.519884  7.419414
## 3  8.495332  7.031415  9.959249
## 4 11.021015  9.520132 12.521898
## 5 13.546698 11.987657 15.105739

3. e)

aenn<- ennustus^2
aenn
##         fit       lwr       upr
## 1  11.86090   3.93973  24.03997
## 2  35.63671  20.42935  55.04771
## 3  72.17067  49.44080  99.18664
## 4 121.46277  90.63292 156.79793
## 5 183.51303 143.70392 228.18335