# Stat1, A3
# MRA

# Загружаем библиотеку
library(psych)

#Загружаем данные
setwd("~/Загрузки/Statistics One")
endur < - read.table('DAA.03.TXT', header=T)

# Строим диаграмму рассеяния (endurance~age)
plot(endur$endurance~endur$age, main='Scatterplot', ylab='Endurance', xlab='Age')

# Добавляем линию регрессии 
abline(lm(endur$endurance~endur$age), col='blue')

# Строим диаграмму рассеяния (endurance~activeyears)
plot(endur$endurance~endur$activeyears, main='Scatterplot', ylab='Endurance', xlab='Active years')

# Добавляем линию регрессии
abline(lm(endur$endurance~endur$activeyears), col='blue')

# Пример простой линейной регрессии Model1 (endurance~age)
model1=lm(endur$endurance~endur$age)
summary(model1)

# Пример простой линейной регрессии Model2 (endurance~activeyears)
model2=lm(endur$endurance~endur$activeyears)
summary(model2)

# Множественная регрессия (2 предиктора)
model3=lm(endur$endurance~endur$age + endur$activeyears)
summary(model3)

# Регрессионный анализ (станд. коэф.)
model1.z=lm(scale(endur$endurance)~scale(endur$age))
summary(model1.z)
model2.z=lm(scale(endur$endurance)~scale(endur$activeyears))
summary(model2.z)
model3.z=lm(scale(endur$endurance)~scale(endur$age) + scale(endur$activeyears))
summary(model3.z)

# Сравнение моделей (1~3 и 2~3)
comp1 = anova(model1.z, model3.z)
comp1
comp2 = anova(model2.z, model3.z)
comp2

# Корреляция
cor(endur[-1])