绘图函数 自定义函数 数据分析实战 线性回归

绘图函数 自定义函数 数据分析实战 线性回归

绘图函数

ls("package:graphics")
demo(graphics)
#使用demo展示graphics
help(package=graphics)
?datasets
library(help = "datasets")
plot (women$height)#使用plot对height进行画图
plot(women$height,women$weight)#使用plot对height与weight进行画图
plot(as.factor(women$height))#使用因子绘图,输出直方图
plot(mtcars$cyl)#不使用因子输出散点图
plot(as.factor(mtcars$cyl))#转换为因子输出直方图
plot(as.factor(mtcars$cyl),mtcars$carb)#因子加数值绘制箱线图
plot(as.factor(mtcars$carb),as.factor(mtcars$cyl))#两个都是因子
#输出的是脊柱图
plot (women$height~ women$weight)#使用~输出两者的关系图
fit <- lm(height~ weight,data=women)#使用lm进行线性回归的分析
plot(fit)#生成四幅图
methods(plot)
#plot函数是一个家族,里面包含了很多很多函数
par()
?par#用来对绘图参数进行设置
plot(as.factor(mtcars$cyl),col=c("red","green","blue"))

自定义函数

cor#直接输入函数名就可以查看某个函数的源代码
mystats <- function(x,na.omit=FALSE) {#函数声明,选项参数,删除缺失值
  if(na.omit) #如果设置了na.omit则x只取不包含na的值
    x <- x[!is.na(x)]
  m <- mean(x)
  n <- length(x)
  s <- sd(x)
  skew <- sum((x-m^3/s^3))/n#偏度值
  kurt <- sum((x-m^4/s^4))/n-3#峰度值
  return(c(n=n,mean=m,stdev=s,skew=skew,kurtosis=kurt))
 }

for (i in 1:10) {print ("Hello,World")}
#for ($i=1;$i<=10;$++) {print "hello,world\n";}
i=1;while(i <= 10) {print ("Hello,World");i=i+1}
i=1;while(i <= 10) {print ("Hello,World");i=i+2;}
score=70;if (score >60 ) {print ("Passed") } else {print ("Failed")}
ifelse( score >60,print ("Passed"),print ("Failed"))

#switch
centre <- function(x, type) {
  switch(type,
         mean = mean(x),
         median = median(x),
         trimmed = mean(x, trim = .1))
}
x <- rcauchy(10)
centre(x, "mean")
centre(x, "median")
centre(x, "trimmed")

数据分析实战

women
?lm
fit <- lm(weight ~ height, data=women)
summary(fit)
women$weight
fitted(fit)

线性回归

#回归:使用自变量来预测因变量
fit <- lm(weight ~ height, data=women)
#使用height预测weight,这个过程称为拟合
summary(fit)#使用summary总结回归分析结果
coefficients(fit)#列出summary中coefficient的结果
women$weight-fitted(fit)#检测残差值
residuals(fit)#直接检测残差值
women1 <- head(women,6)
plot(fit)#使用plot绘制分析结果图
predict(fit,women1)#根据fit对新的数据集进行预测
plot(women$height,women$weight)
plot(women$height,women$weight,
     main="Women Age 30-39", 
     xlab="Height (in inches)", 
     ylab="Weight (in pounds)")
#add the line of best fit
abline(fit)#绘制拟合曲线
#fit2
fit2 <- lm(weight ~ height + I(height^2), data=women)
#线性分析weight与height的关系,weight为因变量,这里我们增加一个二次项
#增加自变量,使得模型复杂
summary(fit2)
summary(fit)
plot(women$height,women$weight,
     main="Women Age 30-39",
     xlab="Height (in inches)",
     ylab="Weight (in lbs)")
lines(women$height,fitted(fit2))
abline(fit)#绘制第一次回归的结果
lines(women$height,fitted(fit2),col="red")#绘制第二次拟合的结果
#fit3
fit3 <- lm (weight~ height+I(height^2)+I(height^3),data=women)
plot(women$height,women$weight)
lines(women$height,fitted(fit))
lines(women$height,fitted(fit2),col="red")
lines(women$height,fitted(fit3),col="blue")
#拟合加深的终止需要对于数据本身有所了解(domain knowledge)
#其次需要多次尝试,避免过拟合和欠拟合
posted @ 2021-04-22 16:00  KONGQer  阅读(167)  评论(0)    收藏  举报