首页 > > 详细

辅导R语言编程、R语言辅导、讲解留学生R编程、R辅导留学生


# load the library we need. This is library package that is used with the book
# Introductory Statistics with R by Peter Dalgaard
library(ISwR)
library(car) # need this package for the vif() command
# set working directory
setwd("~/Dropbox/Penn_State/Stat470/Lectures")
# load the data set we'll work with today; this is in the ISwR package
data(cystfibr)
# let's look at the data set
?cystfibr
head(cystfibr)
str(cystfibr)
# do you know what the difference is between these two types of correlation?
cor(cystfibr, cystfibr$pemax)
cor(cystfibr, cystfibr$pemax, method = "spearman")
pairs(cystfibr)
#create pdf of a figure
pdf("matrix_scatter_covariates.pdf")
pairs(cystfibr, gap = 0, cex.labels = .9)
dev.off()
# if you want to generate a pdf of the figure:
# start with pdf("name of figure")
# end with "dev.off()

boxplot(cystfibr$pemax~cystfibr$sex, col = "blue")
# for more parsimonious coding, you can attache the data set:
attach(cystfibr)
boxplot(pemax~sex, col = "blue")
hist(pemax, col = "blue")
hist(age, col = "red")
summary(cystfibr)
# what other summaries could be useful here?
pdf("example_scatter.pdf")
plot(age, pemax, pch = 19, main = "Maximum Expiratory Pressure and Age",
xlab = "Age in years",
ylab = "Maximum Expiratory Pressure", cex.main = 1.5, cex.lab = 1.5)
abline(lm(pemax~age), col = "green")
lines(supsmu(age, pemax, span = .5), col = "red")
dev.off()
############################################################
# fitting a linear model using all the predictors
mm1 <- lm(pemax~age+sex+height+weight+bmp+fev1+rv+frc+tlc)
summary(mm1)
anova(mm1)
vif(mm1) # need package car
plot(mm1)
plot(mm1$model$weight,resid(mm1), pch = 19)# check linearity with continuous variable weight
abline(h = 0)
############################################################
# fit models using only age and sex
mm.s1 <- lm(pemax~age*sex)
mm.s2 <- lm(pemax~sex+age)
# linear model output
summary(mm.s1)
summary(mm.s2)
# anova tests
anova(mm.s1)
anova(mm.s2)
############ I() function -- useful for transformations
mm <- lm(pemax~height)
summary(mm)
plot(height,resid(mm))
abline(h = 0)
mm <- lm(pemax~height+I(height^2))
summary(mm)
plot(height, resid(mm))
abline(h = 0)
 

联系我们
  • QQ:99515681
  • 邮箱:99515681@qq.com
  • 工作时间:8:00-21:00
  • 微信:codinghelp
热点标签

联系我们 - QQ: 99515681 微信:codinghelp
程序辅导网!