楼主: ywh19860616
26305 92

[问答] 怎么把这个矩阵保存到excel中 [推广有奖]

51
zhangtao 发表于 2012-11-8 15:10:22 |只看作者 |坛友微信交流群
epoh 发表于 2012-11-8 11:30
你给的Uexample必须是square matrix
请看function mat2listw的用法
     http://127.0.0.1:13372/libra ...
epoh老师,您好!
现在的问题是无法读入uexample and sexample这两个数据,
我命令都对,怎么会读不进去呢?
但是输入 fix(Sexample)提示数据读进去了。
数学好就是要天天学

使用道具

52
epoh 发表于 2012-11-8 16:22:28 |只看作者 |坛友微信交流群
zhangtao 发表于 2012-11-8 15:10
epoh老师,您好!
现在的问题是无法读入uexample and sexample这两个数据,
我命令都对,怎么会读不进去 ...
library(splm)
data(Produc, package = "Ecdat")
data(usaww)
write.csv(Produc,"Sexample.csv",row.names = FALSE,col.names = TRUE)
write.csv(usaww,"Uexample.csv",row.names = FALSE,col.names = TRUE)
Sexample=read.csv("Sexample.csv")
Uexample=read.csv("Uexample.csv")
Uexample=data.matrix(Uexample)
fm <- log(gsp) ~ log(pcap) + log(pc) + log(emp) + unemp
## the two standard specifications (SEM and SAR) one with FE
## and the other with RE:
## fixed effects panel with spatial errors
fespaterr <- spml(fm, data = Sexample, listw = mat2listw(Uexample), model="within", spatial.error="b")
summary(fespaterr)
########
Spatial panel fixed effects error model

Call:
spml(formula = fm, data = Sexample, listw = mat2listw(Uexample),
    model = "within", spatial.error = "b")

Residuals:
   Min. 1st Qu.  Median 3rd Qu.    Max.
-0.1250 -0.0238 -0.0035  0.0171  0.1880

Coefficients:
            Estimate Std. Error t-value Pr(>|t|)   
rho        0.5574013  0.0330749 16.8527  < 2e-16 ***
log(pcap)  0.0051438  0.0250109  0.2057  0.83705   
log(pc)    0.2053026  0.0231427  8.8712  < 2e-16 ***
log(emp)   0.7822540  0.0278057 28.1328  < 2e-16 ***
unemp     -0.0022317  0.0010709 -2.0839  0.03717 *  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
#########
## random effects panel with spatial lag
respatlag <- spml(fm, data = Sexample, listw = mat2listw(Uexample), model="random", spatial.error="none", lag=TRUE)
summary(respatlag)
########
Spatial panel random effects ML model

Call:
spml(formula = fm, data = Sexample, listw = mat2listw(Uexample),
    model = "random", lag = TRUE, spatial.error = "none")

Residuals:
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
   1.38    1.57    1.70    1.70    1.80    2.13

Error variance parameters:
    Estimate Std. Error t-value Pr(>|t|)  
phi  21.3175     8.2937  2.5703  0.01016 *

Spatial autoregressive coefficient:
       Estimate Std. Error t-value Pr(>|t|)   
lambda 0.161615   0.029041  5.5651 2.62e-08 ***

Coefficients:
               Estimate  Std. Error t-value  Pr(>|t|)   
(Intercept)  1.65814995  0.15071855 11.0016 < 2.2e-16 ***
log(pcap)    0.01294505  0.02493997  0.5190    0.6037   
log(pc)      0.22555376  0.02163422 10.4258 < 2.2e-16 ***
log(emp)     0.67081075  0.02642113 25.3892 < 2.2e-16 ***
unemp       -0.00579716  0.00089175 -6.5009 7.984e-11 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
zhangtao + 5 + 5 + 5 感谢epoh老师的指导!!!

总评分: 学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

使用道具

53
zhangtao 发表于 2012-11-8 19:43:49 |只看作者 |坛友微信交流群
epoh 发表于 2012-11-8 16:22
library(splm)
data(Produc, package = "Ecdat")
data(usaww)
epoh老师,您好!
为什么加上row.names = FALSE这一行程序就可以
行了呢?您是怎么知道的呢?
非常感谢!
数学好就是要天天学

使用道具

54
epoh 发表于 2012-11-9 18:40:56 |只看作者 |坛友微信交流群
zhangtao 发表于 2012-11-8 19:43
epoh老师,您好!
为什么加上row.names = FALSE这一行程序就可以
行了呢?您是怎么知道的呢?
write.csv预设是 row.names = TRUE,col.names = TRUE
这时候write.csv(Produc,"Sexample.csv")
会把第一行column 1,2,3,....,加进来
      X          state year      pcap      hwy    water     util  ...
1     1        ALABAMA 1970  15032.67  7325.80  1655.68  6051.20  ...
2     2        ALABAMA 1971  15501.94  7525.94  1721.02  6254.98  ...
3     3        ALABAMA 1972  15972.41  7765.42  1764.75  6442.23  ...
造成dimension不对,所以要改成底下:
write.csv(Produc,"Sexample.csv",row.names = FALSE,col.names = TRUE)

编程中,随时都要检查读入的数据是否正确,
以确保程序的运行.
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
zhangtao + 5 + 5 + 5 好的意见建议

总评分: 学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

使用道具

55
zhangtao 发表于 2012-11-9 22:44:06 |只看作者 |坛友微信交流群
epoh 发表于 2012-11-9 18:40
write.csv预设是 row.names = TRUE,col.names = TRUE
这时候write.csv(Produc,"Sexample.csv")
会把第一 ...
graphics.off()
library(strucchange)
library(longmemo) # not used
postscript(file="c:/res-teach/xiamen12-06/figs/fig-9.2.eps",
horizontal=F,width=6,height=6)
epoh老师,您好!
这个postscript(......)和graphics.off()这两个命令是什么意思?

> da=read.table("m-intc7308.txt",header=T)
>  library(fGarch) # Load the package
载入需要的程辑包:timeDate
载入需要的程辑包:timeSeries
载入需要的程辑包:fBasics
载入需要的程辑包:MASS
载入程辑包:‘fBasics’
The following object(s) are masked from ‘package:base’:
    norm
>  intc=log(da[,2]+1)
>  m1=garchFit(intc~garch(1,0),data=intc,trace=F)
>  summary(m1) # Obtain results
错误于summary(m1) :
  在为'summary'函数选择方法时评估'object'参数出了错: 错误: 找不到对象'm1'

>
还有,以上代码错在什么地方? m-intc7308.txt (7.99 KB)

已有 1 人评分学术水平 热心指数 信用等级 收起 理由
epoh + 2 + 2 + 2 鼓励积极发帖讨论

总评分: 学术水平 + 2  热心指数 + 2  信用等级 + 2   查看全部评分

数学好就是要天天学

使用道具

56
epoh 发表于 2012-11-10 17:21:38 |只看作者 |坛友微信交流群
zhangtao 发表于 2012-11-9 22:44
graphics.off()
library(strucchange)
library(longmemo) # not used
graphics.off() : shuts down all open graphics devices
                意思是关闭所有图形窗口,打算将下一个图形写入fig-9.2.eps
postscript() : open a postscript() device with a filename ending in .eps

example:
  graphics.off()
  postscript("test.eps")
  plot(rnorm(100))
  dev.off()

这时候就会产生一个test.eps文件
当然你也可以由图型窗口
file\save as \postscript\....

使用道具

57
epoh 发表于 2012-11-10 17:56:23 |只看作者 |坛友微信交流群
zhangtao 发表于 2012-11-9 22:44
graphics.off()
library(strucchange)
library(longmemo) # not used
da=read.table("m-intc7308.txt",header=F)
intc=log(da[,2]+1)
m1=garchFit(~garch(1,0),data=intc,trace=F)
summary(m1) # Obtain results
Title:
GARCH Modelling

Call:
garchFit(formula = ~garch(1, 0), data = intc, trace = F)

Mean and Variance Equation:
data ~ garch(1, 0)
<environment: 0x08595964>
[data = intc]

Conditional Distribution:
norm

Coefficient(s):
     mu    omega   alpha1  
0.01657  0.01249  0.36345  

Std. Errors:
based on Hessian

Error Analysis:
        Estimate  Std. Error  t value Pr(>|t|)   
mu      0.016570    0.006161    2.689  0.00716 **
omega   0.012490    0.001549    8.061 6.66e-16 ***
alpha1  0.363447    0.131598    2.762  0.00575 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Log Likelihood:
230.2423    normalized:  0.6189309

Description:
Sat Nov 10 17:36:51 2012 by user: pro


Standardised Residuals Tests:
                                Statistic p-Value     
Jarque-Bera Test   R    Chi^2  122.404   0           
Shapiro-Wilk Test  R    W      0.9647628 8.274108e-08
Ljung-Box Test     R    Q(10)  13.72604  0.1858587   
Ljung-Box Test     R    Q(15)  22.31714  0.09975386  
Ljung-Box Test     R    Q(20)  23.88257  0.2475594   
Ljung-Box Test     R^2  Q(10)  12.50025  0.25297     
Ljung-Box Test     R^2  Q(15)  30.11276  0.01152131  
Ljung-Box Test     R^2  Q(20)  31.46404  0.04935483  
LM Arch Test       R    TR^2   22.036    0.0371183   

Information Criterion Statistics:
      AIC       BIC       SIC      HQIC
-1.221733 -1.190129 -1.221861 -1.209182
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
zhangtao + 5 + 5 + 5 好的意见建议

总评分: 学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

使用道具

58
zhangtao 发表于 2012-11-11 19:12:01 |只看作者 |坛友微信交流群
epoh 发表于 2012-11-10 17:56
da=read.table("m-intc7308.txt",header=F)
intc=log(da[,2]+1)
m1=garchFit(~garch(1,0),data=intc, ...
epoh老师,您好!
     在read.csv这个命令中,row.names 和 col.names这两个选项的参数如何设定?
以下帮助我没有看懂,不知说的是什么意思。如果我不想读入行名和列名,row.names 和 col.names这两个选项的参数如何设定?
> read.csv(Sexample.csv, fill = TRUE, header = FALSE,row.names=FALSE, col.names = FALSE, sep = ""))
错误: 意外的')'在"read.csv(Sexample.csv, fill = TRUE, header = FALSE,row.names=FALSE, col.names = FALSE, sep = ""))"里
> fix(Sexample.csv)
> fix(Sexample.csv)
> read.csv(Sexample.csv, fill = TRUE, header = FALSE,row.names=FALSE, col.names = FALSE, sep = "")
错误于read.table(file = file, header = header, sep = sep, quote = quote,  :
  'file'的值必需是字符串或联结
> read.csv("Sexample.csv", fill = TRUE, header = FALSE,row.names=FALSE, col.names = FALSE, sep = "")
错误于read.table(file = file, header = header, sep = sep, quote = quote,  :
  列的数目比列的名字要多
> row.names a vector of row names. This can be a vector giving the actual row names, or a single number giving the column of the table which contains the row names, or character string giving the name of the table column containing the row names.

If there is a header and the first row contains one fewer field than the number of columns, the first column in the input is used for the row names. Otherwise if row.names is missing, the rows are numbered.

Using row.names = NULL forces row numbering. Missing or NULL row.names generate row names that are considered to be ‘automatic’ (and not preserved by as.matrix).

col.names a vector of optional names for the variables. The default is to use "V" followed by the column number.

数学好就是要天天学

使用道具

59
epoh 发表于 2012-11-12 15:41:40 |只看作者 |坛友微信交流群
zhangtao 发表于 2012-11-11 19:12
epoh老师,您好!
     在read.csv这个命令中,row.names 和 col.names这两个选项的参数如何设定?
以 ...
Testcsv.csv   testcsv.rar (206 Bytes) 本附件包括:
  • testcsv.csv

底下是testcsv.csv的数据:
   make   model mpg weight price
1   AMC Concord  22   2930  4099
2   AMC   Pacer  17   3350  4749
3   AMC  Spirit  22   2640  3799
4 Buick Century  20   3250  4816
5 Buick Electra  15   4080  7827

#read.csv("testcsv.csv", header = TRUE)
#header=T的意思是第一行被认为是header(names of the variables ),而不是数据.
a1=read.csv("testcsv.csv", header = TRUE)
a1
   make   model mpg weight price
1   amc concord  22   2930  4099
2   amc   oacer  17   3350  4749
3   amc  spirit  22   2640  3799
4 buick century  20   3250  4816
5 buick electra  15   4080  7827

##read.csv("testcsv.csv", header =  FALSE)
#header= FALSE的意思是第一行被认为是数据.
#col.names a vector of optional names for the variables.
#The default is to use "V" followed by the column number.
a2=read.csv("testcsv.csv", header = FALSE)
a2
     V1      V2  V3     V4    V5
1  make   model mpg weight price
2   amc concord  22   2930  4099
3   amc   oacer  17   3350  4749
4   amc  spirit  22   2640  3799
5 buick century  20   3250  4816
6 buick electra  15   4080  7827

#read.csv(),预设就是row.names=NULL
#所以同a1
a3=read.csv("testcsv.csv", header = TRUE,row.names=NULL)
a3
   make   model mpg weight price
1   amc concord  22   2930  4099
2   amc   oacer  17   3350  4749
3   amc  spirit  22   2640  3799
4 buick century  20   3250  4816
5 buick electra  15   4080  7827
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
zhangtao + 5 + 5 + 5 观点有启发

总评分: 学术水平 + 5  热心指数 + 5  信用等级 + 5   查看全部评分

使用道具

60
zhangtao 发表于 2012-11-12 21:25:44 |只看作者 |坛友微信交流群
epoh 发表于 2012-11-12 15:41
Testcsv.csv  
底下是testcsv.csv的数据:
   make   model mpg weight price
datana.rar (1.7 KB) 本附件包括:
  • datana.xls

epoh老师,您好!
     附件中的数据缺失了1990-1993四年的数据,想用插值法补齐,
您看在R中如何操作?
非常感谢!

数学好就是要天天学

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-1 12:18