Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何从数据帧预测新行?_R_Dataframe_Lm_Predict - Fatal编程技术网

R 如何从数据帧预测新行?

R 如何从数据帧预测新行?,r,dataframe,lm,predict,R,Dataframe,Lm,Predict,这是开放数据。请看一下数据和我的代码,试着告诉我我做错了什么。以下是数据- 我只是想根据这些数据预测下一个10行左右的数据,但我总是出错 这是到目前为止我的代码- df=read.csv(file="sampledata.csv", header=FALSE, row.names=NULL) df <- df[-c(21),] start <- length(df) *.2 train = df[,start:length(df)] test = df[,1:start] #d

这是开放数据。请看一下数据和我的代码,试着告诉我我做错了什么。以下是数据-

我只是想根据这些数据预测下一个10行左右的数据,但我总是出错

这是到目前为止我的代码-

df=read.csv(file="sampledata.csv", header=FALSE, row.names=NULL)

df <- df[-c(21),]

start <- length(df) *.2

train = df[,start:length(df)]
test = df[,1:start]
#df <- data.frame(t(df))

train <- data.frame(t(train))
test <- data.frame(t(test))

#library(rpart)
#fit <- rpart(X1 ~ ., method='anova', data=df)

#View(df)

#predict(fit, type="prob")

#df$Prediction <- predict(lm(X1~., data=df))

fit <- lm(V1~., data=data.frame(t(train)))
#test <- data.frame(t(test))

#predictions <- data.frame(matrix(nrow=length(test), ncol=20))
#predictions <- as.matrix(predictions)
#predictions <- as.integer(predictions)
#dim(predictions) <- c(length(test), 20)
#test <- data.frame(t(test))
#predictions <- data.frame(predictions)

predict(fit, newdata=test)
请注意,它打印出来的内容与上面的内容不同。我手动构建了这些数据。我需要3到10行20个不同的预测值,我还需要看到预测数字的概率


祝你好运,谢谢你

首先,只有在
write.csv
中,
row.names
参数的预期值才是逻辑的(TRUE或FALSE),请查看
write.csv
中的定义。在
read.csv
中,根据
?read.csv
,此参数的预期值应该是文件中包含行名的列的行名或索引位置的向量(本例中为第一个)。因此,请阅读您的文件,如:

df1 <- read.csv(file="sampledata.csv", header=T, row.names=1)

df1如果希望预测因变量(X1)的新值,则需要为预测值(X2,X3,…)提供新值。
但是,您的predictions data.frame(应该包含新的预测器值)中只有NA条目,因此无法从中预测X1。
从数据中不清楚您提供的“下十列”(在您的情况下,实际上是行)可能是什么样子,即您的预测值可能是什么


当然,你可以简单地从你的观察结果中为你的预测值重新采样“新”值。但我不认为你会对由此产生的预测感兴趣。所以,第一个问题是:什么情况(X2,X3,…)会让你感兴趣?

这不是答案,也没有帮助。我试图预测像X1这样的列,但我想使用X1到长度(df)来预测一个新列。知道怎么做吗?等等,我搞错了。请看编辑。我忘了我创建的csv文件已经被转置了。@Poppins586只有一个问题,在你的csv文件中,16000列代表什么:样本还是变量?样本。这是一个训练集。先生,要得到想要的结果,你的数据必须是正确的格式。样本应按行运行,变量应按列运行。尽管进行了编辑,您的问题仍然生成16000列x20行数据。frame:因为您遗漏了
df1
df1 <- read.csv(file="sampledata.csv", header=T, row.names=1)
df1 <- data.frame(t(df1))
fit <- lm(V1~., data=df1)