(R-note)R笔记

1.图像标题在这里插入图片描述
在这里插入图片描述
转自:https://blog.csdn.net/kMD8d5R/article/details/81844126
2.R语言中fitted()和predict()的区别
在这里插入图片描述
3.Q2: 为什么预测变量x的名称(代码一)从x变成x_date(代码二),predict()得到的yf从30个数据变成10个(与响应变量个数相同)数据?

代码一:

#作为观测的响应变量y值
ConfirmNumIcr <- c(42744, 40224, 37162,
34594, 31197, 28060, 24363, 20471,
17238, 14411)
#作为观测的预测变量x值
x <- 1:length(y)
days <- 30
#建立模型;已证明该模型p-value最小
model1 <-lm.fit3_2 <- lm(ConfirmNumIcr~I(x2)+I(x3))
new.data <- data.frame(x=seq(1,days,1)) #predict variable
yf <- predict(model1, newdata = new.data,se = TRUE)
print(“model_date:”)
length(new.data)
new.data
print(“model_fitted_y”)
length(yf)
yf

结果一:
在这里插入图片描述

代码二:

#作为观测的响应变量y值
ConfirmNumIcr <- c(42744, 40224, 37162,
34594, 31197, 28060, 24363, 20471,
17238, 14411)
#作为观测的预测变量x值
x_date <- 1:length(y)
days <- 30
#画出数据离散点 注:plot()必须加上type=""参数才能画出线
#plot(x_date, ConfirmNumIcr,type = ‘b’,xlim = c(1,days+1),ylim =c(0,5000),xlab=‘Days since Jan 23’, ylab=’#Newly confirmed cases’,cex = 1.5 )
#points(x_date[length(ConfirmNumIcr)], ConfirmNumIcr[length(ConfirmNumIcr)],col=“black”,pch=19,cex=2)
#建立模型;已证明该模型p-value最小
model1 <-lm.fit3_2 <- lm(ConfirmNumIcr~I(x_date2)+I(x_date3))
#model1 <- lm.fit3_2
#求出拟合模型
#为什么要用data.frame?
new.data <- data.frame(x=seq(1,days,1)) #predict variable
#predict()函数的意义:给定一个模型model1,给出横座标向量,生成纵座标向量,se=TRUE即指给出Standard ERROR
#Q:model_fitted_y也是有下个值的向量,如何lines()出来?
yf <- predict(model1, newdata = new.data,se = TRUE)
print(“model_date:”)
length(new.data)
new.data
print(“model_fitted_y”)
length(yf)
yf

结果二;
在这里插入图片描述
原因:
model拟合的预测变量名称x 要与 预测函数predict()里的newdata表格的参数x=seq()名称要一致
在这里插入图片描述

4.画图显示中文字符
画图显示中文:
par(family=‘STKaiti’)
OS X 下,用的是 STXihei,Ubuntu 你自己找到文泉驿之类的字体的 family name 改进去就行了。

两种改法,全局的和局部的:
par(family=‘STXihei’)
plot(d, family=‘STXihei’)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章